{"id":842,"date":"2010-12-23T23:55:27","date_gmt":"2010-12-23T15:55:27","guid":{"rendered":"http:\/\/leegorous.net\/blog\/?p=842"},"modified":"2012-03-03T19:15:14","modified_gmt":"2012-03-03T11:15:14","slug":"load-js-dynamically","status":"publish","type":"post","link":"http:\/\/leegorous.net\/blog\/2010\/12\/23\/load-js-dynamically\/","title":{"rendered":"\u52a8\u6001\u52a0\u8f7d\u811a\u672c"},"content":{"rendered":"<p>\u52a8\u6001\u88c5\u8f7d\u811a\u672c\u662f\u5f88\u7b80\u5355\u7684\uff0c\u4e0d\u662f\u901a\u8fc7 script \u6807\u7b7e\u5b9e\u73b0\uff0c\u5c31\u662f\u901a\u8fc7 XMLHTTP \u8bf7\u6c42\u5185\u5bb9\u4e4b\u540e\u6267\u884c\u3002\u540e\u8005\u4f1a\u53d7\u5230\u540c\u57df\u9650\u5236\uff0c\u800c\u5904\u4e8e\u5404\u79cd\u539f\u56e0\uff0c\u811a\u672c\u8de8\u57df\u5f15\u7528\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u8fd9\u6837\u5bf9\u6bd4\u8d77\u6765\u524d\u8005\u9002\u5e94\u6027\u66f4\u5e7f\u3002<\/p>\n<p>\u65e2\u53ef\u4ee5\u7528\u5f88 DOM \u7684\u65b9\u5f0f\u521b\u5efa script \u6807\u7b7e\uff1a<\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nfunction getScript(path, callback) {\r\n    var el = document.createElement(&quot;script&quot;);\r\n    if (callback) {\r\n        el.onload = el.onreadystatechange = function() {\r\n            if (!this.readyState || this.readyState === &#039;complete&#039; || \r\n                    this.readyState === &#039;loaded&#039;) {\r\n                el.onload = el.onreadystatechange = null;\r\n                callback();\r\n            }\r\n        };\r\n    }\r\n    el.src = path;\r\n    document.getElementsByTagName(&quot;HEAD&quot;)[0].appendChild(el);\r\n}\r\n<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <a href=\"https:\/\/developer.mozilla.org\/en\/document.write\" target=\"_blank\">document.write()<\/a>:<!--more--><\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nvar writeScript = function(path) {\r\n    document.write(&quot;&lt;script src=\\&quot;&quot;+path+&quot;\\&quot;  type=\\&quot;text\/javascript\\&quot; &gt;&lt;\\\/script&gt;&quot;);\r\n}\r\n<\/pre>\n<p>\u4e0d\u8fc7\u5c31\u4e0d\u80fd\u6709 callback\uff0c\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u52a0\u8f7d\u7ed3\u675f\uff0c\u5e76\u4e14\u4e0d\u662f\u4efb\u4f55\u65f6\u5019\u90fd\u80fd write \u7684\u3002<\/p>\n<p>\u8fd9\u4e2a\u770b\u4f3c\u5f88\u5165\u95e8\u7ea7\u522b\u7684\u51fd\u6570\uff0c\u7528\u5f97\u6bd4\u8f83\u5c11\uff0c\u6211\u4e00\u76f4\u4ee5\u4e3a\u5b83\u4f1a\u5c06\u5185\u5bb9\u5199\u5165\u5230\u6587\u6863\u6700\u540e\uff0c\u8ddf document.body.append \u5dee\u4e0d\u591a\u7684\u6548\u679c\uff0c\u5f53\u6574\u4e2a\u6587\u6863\u88ab\u6e05\u7a7a\u4e4b\u540e\uff0c\u7ec8\u4e8e\u610f\u8bc6\u5230\u8fd9\u662f\u4e2a\u95ee\u9898\uff0c\u539f\u6765\u4e00\u76f4\u90fd\u5ffd\u7565\u4e86 document stream \u7684\u6982\u5ff5\u3002<\/p>\n<p>\u6587\u6863\u52a0\u8f7d\u7684\u8fc7\u7a0b\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u6d41\uff0c\u4ece\u5934\u6d41\u5230\u6587\u6863\u7ed3\u675f\uff0c\u6d41\u4e2d\u7684\u8282\u70b9\u662f\u6309\u6587\u672c\u987a\u5e8f\u8fdb\u884c\u6e32\u67d3\uff0c\u6240\u5d4c\u5165\u7684\u811a\u672c\u4e5f\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684\u3002\u57fa\u4e8e\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u7406\u89e3\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u5e38\u8bf4\u7684\u4e00\u9879\u9875\u9762\u4f18\u5316\uff1a\u5c06\u811a\u672c\u653e\u5230\u6587\u6863\u6700\u540e\u4e0d\u5f71\u54cd\u9875\u9762\u5185\u5bb9\u7684\u6e32\u67d3\u3002\u800c\u6d41\u4e00\u65e6 close\uff0c\u518d\u6b21 open \u65f6\uff0c\u5c31\u4f1a\u91cd\u65b0\u6784\u9020\u6574\u4e2a\u6587\u6863\u6d41\uff0c\u539f\u6765\u7684\u6240\u6709\u4e1c\u897f\u90fd\u88ab\u6e05\u7a7a\u4e86\uff0c\u800c\u8fd9\u4e2a\u5e94\u8be5\u4e0d\u662f\u5927\u591a\u6570 AJAX \u5e94\u7528\u6240\u9700\u8981\u7684\u6548\u679c\u3002\u6240\u4ee5 document.write \u662f\u7528\u5728\u9875\u9762\u8fd8\u6ca1\u6709\u6e32\u67d3\u5b8c\u6bd5\u4e4b\u524d\u52a8\u6001\u4fee\u6539\u6587\u6863\u6d41\u5185\u5bb9\u7684\uff0c\u5f53\u9875\u9762\u52a0\u8f7d\u5b8c\u6bd5\u5c31\u4e0d\u8be5\u518d\u7528\u4e86\u3002<\/p>\n<p>\u867d\u7136\u4e0d\u5e94\u8be5\u5728\u672c\u9875\u9762\u52a0\u8f7d\u7ed3\u675f\u4e4b\u540e\u8c03\u7528 document.write\uff0c\u4f46\u53ef\u4ee5\u5bf9\u5b50\u7a97\u53e3\u8fd9\u4e48\u505a\uff0c\u8fd9\u4e5f\u662f\u70b9\u51fb\u8fd0\u884c\u5c31\u6253\u5f00\u4e00\u4e2a\u65b0\u7a97\u53e3\u5e76\u6267\u884c\u4e00\u4e9b\u811a\u672c\u7684\u4e00\u79cd\u5b9e\u73b0\uff1a<\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nvar loadInFrame = function() {\r\n    var frame = document.createElement(&quot;iframe&quot;);\r\n    document.body.appendChild(frame);\r\n    var doc = frame.contentWindow.document;\r\n    doc.open();\r\n    doc.write(&quot;Demo here &lt;script type=\\&quot;text\/javascript\\&quot; &gt;alert(&#039;dlrow olleh&#039;);&lt;\\\/script&gt;&quot;);\r\n    doc.close();\r\n}\r\n<\/pre>\n<p>\u626f\u5b8c document stream\uff0c\u56de\u5230\u52a8\u6001\u52a0\u8f7d\u811a\u672c\u7684\u95ee\u9898\u4e0a\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u6bd4\u8f83\u65e0\u804a\u7684\u7ec6\u8282\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u52a8\u6001\u88c5\u8f7d\u4e00\u4e2a image \u662f\u4e0d\u9700\u8981 append \u5230 DOM tree \u4e2d\u5c31\u4f1a\u89e6\u53d1\u88c5\u8f7d\u7684\u3002<\/p>\n<pre name=\"code\" class=\"brush: js\">\r\nnew Image().src = &quot;path\/to\/image.png&quot;;\r\n<\/pre>\n<p>\u4f46\u5bf9\u4e8e script \u8282\u70b9\u662f\u5fc5\u987b\u63d2\u5165\u5230 DOM tree \u4e2d\u624d\u4f1a\u5f00\u59cb\u52a0\u8f7d\u7684\u3002<\/p>\n<p>\u901a\u8fc7\u521b\u5efa script \u6807\u7b7e\u7684\u65b9\u6cd5\u52a0\u8f7d\u811a\u672c\u662f\u5f02\u6b65\u6267\u884c\u7684\u3002\u56e0\u6b64\u5982\u679c\u6709\u591a\u4e2a\u811a\u672c\u9700\u8981\u52a0\u8f7d\u5e76\u4e14\u662f\u5f7c\u6b64\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\u7684\uff0c\u5219\u9700\u8981\u4f9d\u6b21\u987a\u5e8f\u6267\u884c getScript\u3002<\/p>\n<p>PS\uff1a\u5b9e\u6d4b\u53d1\u73b0 chrome \u52a8\u6001\u6dfb\u52a0 script \u6807\u7b7e\u52a0\u8f7d\u6bd4\u8f83\u6162\uff0c\u662f\u6587\u4ef6\u6d41\u65b9\u5f0f\u52a0\u8f7d\u540c\u6837\u6570\u91cf\u811a\u672c\u7684\u5341\u51e0\u500d\u7684\u65f6\u95f4\uff0c\u9f9f\u901f\uff0c\u4f30\u8ba1\u662f\u5bf9\u9875\u9762\u88c5\u8f7d\u7684\u8fc7\u7a0b\u505a\u8fc7\u4f18\u5316\u4e86\uff0c\u52a8\u6001\u52a0\u8f7d\u7528\u5f97\u6bd4\u8f83\u5c11\u5c31\u4e0d\u82b1\u8d39\u7cbe\u529b\u4f18\u5316\u4e86\u3002\u5176\u5b83\u6d4f\u89c8\u5668\u4e5f\u4f1a\u7565\u6162\u4e00\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u88c5\u8f7d\u811a\u672c\u662f\u5f88\u7b80\u5355\u7684\uff0c\u4e0d\u662f\u901a\u8fc7 script \u6807\u7b7e\u5b9e\u73b0\uff0c\u5c31\u662f\u901a\u8fc7 XMLHTTP \u8bf7\u6c42\u5185\u5bb9\u4e4b\u540e\u6267\u884c\u3002\u540e\u8005\u4f1a\u53d7 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/leegorous.net\/blog\/2010\/12\/23\/load-js-dynamically\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u52a8\u6001\u52a0\u8f7d\u811a\u672c\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[102],"tags":[9],"_links":{"self":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/842"}],"collection":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":9,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":1058,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/posts\/842\/revisions\/1058"}],"wp:attachment":[{"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leegorous.net\/blog\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}