{"id":1251,"date":"2010-05-24T22:17:03","date_gmt":"2010-05-24T14:17:03","guid":{"rendered":"http:\/\/nan.im\/blog\/?p=1251"},"modified":"2010-05-24T22:17:03","modified_gmt":"2010-05-24T14:17:03","slug":"%e3%80%90drupal%e3%80%91%e5%90%8e%e5%8f%b0-php-%e4%b8%8e%e5%89%8d%e7%ab%af-javascript-%e4%b9%8b%e9%97%b4%e7%9a%84%e6%95%b0%e6%8d%ae%e4%bc%a0%e9%80%92","status":"publish","type":"post","link":"http:\/\/nan.im\/blog\/1251","title":{"rendered":"\u3010Drupal\u3011\u540e\u53f0 PHP \u4e0e\u524d\u7aef JavaScript \u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012"},"content":{"rendered":"<p>\u5728 Drupal \u6a21\u5757\u6216\u4e3b\u9898\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u6709\u9700\u8981\u4f20\u9012\u4e00\u4e9b\u503c\u7ed9 JavaScript \u7684\u60c5\u51b5\uff0c\u5927\u6982\u6709\u51e0\u79cd\u65b9\u6cd5\u4e00\u662f\u53ef\u4ee5\u76f4\u63a5\u5728\u6a21\u677f\u91cc\u9762\u5199 JavaScript \u7136\u540e\u5d4c PHP \u8fdb\u53bb\uff0c\u4e5f\u53ef\u4ee5\u7528 Drupal \u7684\u63a5\u53e3\u76f4\u63a5\u8f93\u51fa JavaScript \u5230\u9875\u9762\u7684 head \u4fe1\u606f\u4e2d\u53bb\uff0c\u5176\u5b9e\u6211\u81ea\u5df1\u4e4b\u524d\u4e00\u76f4\u662f\u76f4\u63a5\u5728\u6a21\u677f\u91cc\u9762\u5199 JS \u5d4c\u5957 PHP \u7684\uff0c\u540e\u6765\u53d1\u73b0 Drupal \u73b0\u6709\u7684 drupal_add_js \u63d0\u4f9b\u4e86\u4f20\u9012\u53d8\u91cf\u7684\u63a5\u53e3\u5177\u4f53\u5982\u4e0b\u2026\u2026<!--more--><\/p>\n<p>\u4e0b\u9762\u662f\u7b80\u5355\u7684\u5b9e\u73b0\u6b65\u9aa4\uff0c\u76ee\u7684\u662f\u4f20\u9012 drupal \u7684 basePath \u7ed9 JavaScript \u4f7f\u7528\uff0c\u5f53\u7136\u8fd9\u5728 Drupal6 \u4e2d\u662f\u5df2\u7ecf\u9ed8\u8ba4\u4f20\u9012\u7684\uff0c\u5927\u5bb6\u53ef\u4ee5\u4e3e\u4e00\u53cd\u4e09\u6765\u4f7f\u7528\uff1a<\/p>\n<pre lang=\"php\" line=\"1\">\n\/\/\u9996\u5148\u662f\u5728\u6a21\u5757\u6587\u4ef6\u6216\u8005\u662f\u4e3b\u9898\u7684 template \u6587\u4ef6\u4e2d\u52a0\u5165\u5982\u4e0b\u4ee3\u7801\uff1a\ndrupal_add_js(array('basePath' => $GLOBALS['base_path']), 'setting');\n\n\/\/\u7136\u540e\u4f60\u4f1a\u53d1\u73b0\u5728\u9875\u9762\u7684 script \u8f93\u51fa\u90e8\u5206\u4f1a\u6709\u4ee5\u4e0b\u4fe1\u606f\u8f93\u51fa\uff1a\nDrupal.extend({ settings:{\"basePath\": \"\/drupal\/\"} });\n\n\/\/\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u5728\u9875\u9762\u5f15\u5165\u7684 JavaScript \u6587\u4ef6\u4e2d\u76f4\u63a5\u8c03\u7528\u8be5\u5bf9\u8c61\u4e86\u5982\uff1a\nalert(Drupal.settings['basePath']);\n<\/pre>\n<p>\u975e\u5e38\u65b9\u4fbf\u4e3b\u8981\u8d77\u4f5c\u7528\u7684\u662f <a href=\"http:\/\/api.drupal.org\/api\/function\/drupal_add_js\/\">drupal_add_js<\/a> \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570 setting \u4ed6\u544a\u8bc9 drupal \u7cfb\u7edf\u8fd9\u6bb5 js \u662f\u8bbe\u7f6e\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5176\u4ed6\u4f7f\u7528\u65b9\u6cd5\u8bf7\u8bbf\u95ee drupal \u7684\u7ebf\u4e0a api \u51fd\u6570\u624b\u518c\u3002\u5176\u5b9e\u5728\u53ea\u4f20\u9012\u4e00\u4e2a\u53d8\u91cf\u65f6\u8fd9\u79cd\u65b9\u6cd5\u5e26\u6765\u7684\u4f18\u52bf\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u5982\u679c\u6709\u82e5\u5e72\u4e2a\u6a21\u5757\u9700\u8981\u4f20\u9012\u591a\u4e2a\u53d8\u91cf\u7ed9 JavaScript \u7684\u65f6\u5019 drupal_add_js \u51fd\u6570\u4f1a\u81ea\u52a8\u5c06\u4ed6\u4eec\u5408\u5e76\u5230\u4e00\u8d77\u5728\u9875\u9762\u5934\u90e8\u8f93\u51fa\uff0c\u53ea\u8c03\u7528\u4e00\u4e2a Drupal.extend() \u65b9\u6cd5\u4e3a Drupal \u5bf9\u8c61\u589e\u52a0\u989d\u5916\u7684\u503c\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u5728\u5199\u6a21\u5757\u6216\u6a21\u677f\u7684\u65f6\u5019\u5c3d\u91cf\u9075\u5faa Drupal \u63d0\u4f9b\u7ed9\u6211\u4eec\u7684\u73b0\u6709\u7684 api \u8fd9\u5c06\u8ba9\u4f60\u7684\u5de5\u4f5c\u4e8b\u534a\u529f\u500d\u540c\u65f6\u4e5f\u8ba9\u4f60\u7684\u9879\u76ee\u66f4\u5bb9\u6613\u7ef4\u62a4\u4ee3\u7801\u66f4\u5177\u6709\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Drupal \u6a21\u5757\u6216\u4e3b\u9898\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u6709\u9700\u8981\u4f20\u9012\u4e00\u4e9b\u503c\u7ed9 JavaScript \u7684\u60c5\u51b5\uff0c\u5927\u6982\u6709\u51e0\u79cd\u65b9\u6cd5\u4e00\u662f\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,24,31,47,57,120,76],"class_list":["post-1251","post","type-post","status-publish","format-standard","hentry","category-tech","tag-code","tag-develop","tag-drupal","tag-js","tag-module","tag-php","tag-theme"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/comments?post=1251"}],"version-history":[{"count":0,"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/posts\/1251\/revisions"}],"wp:attachment":[{"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/media?parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/categories?post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nan.im\/blog\/wp-json\/wp\/v2\/tags?post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}