{"id":3165,"date":"2024-11-10T17:39:48","date_gmt":"2024-11-10T09:39:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3165\/"},"modified":"2024-11-10T17:39:48","modified_gmt":"2024-11-10T09:39:48","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8js%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3165\/","title":{"rendered":"\u5982\u4f55\u8c03\u7528js\u6587\u4ef6"},"content":{"rendered":"<blockquote><p>\n  \u7f51\u9875\u8c03\u7528 js \u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e94\u79cd\uff1a\u901a\u8fc7 \u6807\u7b7e\u5305\u542b js \u6587\u4ef6\u4f7f\u7528 defer \u5c5e\u6027\u5ef6\u8fdf\u6267\u884c js \u811a\u672c\u4f7f\u7528 async \u5c5e\u6027\u5f02\u6b65\u6267\u884c js \u811a\u672c\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u8c03\u7528 js \u51fd\u6570\u5c06 js \u4ee3\u7801\u4fdd\u5b58\u4e3a\u5916\u90e8\u6587\u4ef6\u5e76\u901a\u8fc7 \u6807\u7b7e\u8c03\u7528\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805541725391.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u8c03\u7528js\u6587\u4ef6\u63d2\u56fe\" alt=\"\u5982\u4f55\u8c03\u7528js\u6587\u4ef6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8c03\u7528 JS \u6587\u4ef6<\/strong><\/p>\n<p>\u5728\u7f51\u9875\u4e2d\u8c03\u7528 JS \u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u7684\u7f51\u9875\u5177\u6709\u4ea4\u4e92\u6027\u548c\u52a8\u6001\u6027\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8c03\u7528 JS \u6587\u4ef6\uff1a<\/p>\n<p><strong>1. &lt;script&gt; \u6807\u7b7e&lt;\/script&gt;<\/strong><\/p>\n<p>\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\u5c06 JS \u6587\u4ef6\u5305\u542b\u5230 HTML \u6587\u6863\u4e2d\uff1a&lt;\/script&gt;<\/p>\n<pre>&lt;script src=\"myScript.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>src \u5c5e\u6027\u6307\u5b9a JS \u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n<li>\u8be5\u6807\u7b7e\u5fc5\u987b\u653e\u7f6e\u5728 \u6216 \u5143\u7d20\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>2. defer \u5c5e\u6027<\/strong><\/p>\n<p>defer \u5c5e\u6027\u53ef\u4ee5\u8ba9\u6d4f\u89c8\u5668\u5728\u52a0\u8f7d\u5b8c HTML \u6587\u6863\u540e\u624d\u6267\u884c JS \u811a\u672c\uff0c\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u9875\u9762\u7684\u52a0\u8f7d\u901f\u5ea6\uff1a<\/p>\n<pre>&lt;script src=\"myScript.js\" defer&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. async \u5c5e\u6027<\/strong><\/p>\n<p>async \u5c5e\u6027\u5141\u8bb8\u811a\u672c\u5728 HTML \u6587\u6863\u5176\u4f59\u90e8\u5206\u52a0\u8f7d\u65f6\u5f02\u6b65\u6267\u884c\uff0c\u4f46\u5b83\u4e0d\u4fdd\u8bc1\u811a\u672c\u7684\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n<pre>&lt;script src=\"myScript.js\" async&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u6765\u8c03\u7528 JS \u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;button onclick=\"myFunction()\"&gt;\u70b9\u51fb\u6211&lt;\/button&gt;\n\n&lt;script&gt;\n  function myFunction() {\n    \/\/ \u4f60\u7684\u4ee3\u7801\n  }\n&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u5916\u90e8\u6587\u4ef6<\/strong><\/p>\n<p>\u5982\u679c JS \u4ee3\u7801\u91cf\u8f83\u5927\uff0c\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u4e3a\u5355\u72ec\u7684\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\u8c03\u7528\uff1a&lt;\/script&gt;<\/p>\n<pre>&lt;script src=\"myExternalScript.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd JS \u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528 defer \u6216 async \u5c5e\u6027\u4ee5\u4f18\u5316\u52a0\u8f7d\u901f\u5ea6\u3002<\/li>\n<li>\u907f\u514d\u5728 \u5143\u7d20\u4e2d\u8fc7\u591a\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\uff0c\u56e0\u4e3a\u5b83\u4f1a\u51cf\u6162\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u3002&lt;\/script&gt; <\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u8c03\u7528js\u6587\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u9875\u8c03\u7528 js \u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e94\u79cd\uff1a\u901a\u8fc7 \u6807\u7b7e\u5305\u542b js \u6587\u4ef6\u4f7f\u7528 defer \u5c5e\u6027\u5ef6\u8fdf\u6267\u884c js \u811a\u672c\u4f7f\u7528 async \u5c5e\u6027\u5f02\u6b65\u6267\u884c js \u811a\u672c\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u8c03\u7528 js \u51fd\u6570\u5c06 js \u4ee3\u7801\u4fdd\u5b58\u4e3a\u5916\u90e8\u6587\u4ef6\u5e76\u901a\u8fc7 \u6807\u7b7e\u8c03\u7528 \u5982\u4f55\u8c03\u7528 JS \u6587\u4ef6 \u5728\u7f51\u9875\u4e2d\u8c03\u7528 JS \u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u7684\u7f51\u9875\u5177\u6709\u4ea4\u4e92\u6027\u548c\u52a8\u6001\u6027\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8c03\u7528 JS \u6587\u4ef6\uff1a 1. &lt;script&gt; \u6807\u7b7e&lt;\/script&gt; \u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 &lt;script&gt; \u6807\u7b7e\u5c06 JS \u6587\u4ef6\u5305\u542b\u5230 HTML \u6587\u6863\u4e2d\uff1a&lt;\/script&gt; &lt;script src=&#8221;myScript.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 src \u5c5e\u6027\u6307\u5b9a JS \u6587\u4ef6\u7684\u8def\u5f84\u3002 \u8be5\u6807\u7b7e\u5fc5\u987b\u653e\u7f6e\u5728 \u6216 \u5143\u7d20\u4e2d\u3002 2. defer \u5c5e\u6027 defer \u5c5e\u6027\u53ef\u4ee5\u8ba9\u6d4f\u89c8\u5668\u5728\u52a0\u8f7d\u5b8c HTML \u6587\u6863\u540e\u624d\u6267\u884c JS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3165"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}