{"id":3210,"date":"2024-11-10T09:14:02","date_gmt":"2024-11-10T01:14:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3210\/"},"modified":"2024-11-10T09:14:02","modified_gmt":"2024-11-10T01:14:02","slug":"js%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e9%a1%b5%e9%9d%a2%e8%b7%b3%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3210\/","title":{"rendered":"js\u5982\u4f55\u5224\u65ad\u9875\u9762\u8df3\u8f6c"},"content":{"rendered":"<blockquote><p>\n  javascript \u63d0\u4f9b\u56db\u79cd\u65b9\u6cd5\u68c0\u6d4b\u9875\u9762\u8df3\u8f6c\uff1awindow.onbeforeunload \u4e8b\u4ef6\uff1a\u5728\u9875\u9762\u5378\u8f7d\u524d\u89e6\u53d1\u3002window.onunload \u4e8b\u4ef6\uff1a\u5728\u9875\u9762\u5b9e\u9645\u5378\u8f7d\u540e\u89e6\u53d1\u3002history api \u7684 popstate \u4e8b\u4ef6\u76d1\u542c\u5668\uff1a\u68c0\u6d4b\u5386\u53f2\u8bb0\u5f55\u53d8\u5316\u3002mutationobserver\uff1a\u76d1\u542c dom \u66f4\u6539\uff0c\u68c0\u6d4b\u9875\u9762\u5378\u8f7d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807125966356.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5224\u65ad\u9875\u9762\u8df3\u8f6c\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5224\u65ad\u9875\u9762\u8df3\u8f6c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u9875\u9762\u8df3\u8f6c<\/strong><\/p>\n<p>JavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u68c0\u6d4b\u9875\u9762\u8df3\u8f6c\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. window.onbeforeunload \u4e8b\u4ef6<\/strong><\/p>\n<p>\u6b64\u4e8b\u4ef6\u5728\u6d4f\u89c8\u5668\u68c0\u6d4b\u5230\u9875\u9762\u5373\u5c06\u5378\u8f7d\u65f6\u89e6\u53d1\u3002\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u6355\u83b7\u6b64\u4e8b\u4ef6\uff0c\u5e76\u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u524d\u6267\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>window.onbeforeunload = function(e) {\n  \/\/ \u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u524d\u6267\u884c\u64cd\u4f5c\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. window.onunload \u4e8b\u4ef6<\/strong><\/p>\n<p>\u6b64\u4e8b\u4ef6\u5728\u9875\u9762\u5b9e\u9645\u5378\u8f7d\u65f6\u89e6\u53d1\u3002\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u6355\u83b7\u6b64\u4e8b\u4ef6\uff0c\u5e76\u6267\u884c\u9875\u9762\u8df3\u8f6c\u540e\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>window.onunload = function(e) {\n  \/\/ \u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u540e\u6267\u884c\u64cd\u4f5c\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. history API<\/strong><\/p>\n<p>JavaScript \u7684\u5386\u53f2\u8bb0\u5f55 API \u63d0\u4f9b\u4e86 pushState() \u548c replaceState() \u65b9\u6cd5\uff0c\u5141\u8bb8\u60a8\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u66f4\u65b0\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u3002\u5f53\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u4e0d\u4f1a\u89e6\u53d1 window.onbeforeunload \u6216 window.onunload \u4e8b\u4ef6\u3002\u8981\u68c0\u6d4b\u5386\u53f2\u8bb0\u5f55\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528 popstate \u4e8b\u4ef6\u76d1\u542c\u5668\uff1a<\/p>\n<pre>window.addEventListener('popstate', function(e) {\n  \/\/ \u5728\u5386\u53f2\u8bb0\u5f55\u53d1\u751f\u53d8\u5316\u65f6\u6267\u884c\u64cd\u4f5c\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. MutationObserver<\/strong><\/p>\n<p>MutationObserver \u662f\u4e00\u79cd JavaScript API\uff0c\u5141\u8bb8\u60a8\u76d1\u89c6 DOM \u4e2d\u7684\u66f4\u6539\u3002\u53ef\u4ee5\u901a\u8fc7\u5728 body \u5143\u7d20\u4e0a\u521b\u5efa\u4e00\u4e2a\u89c2\u5bdf\u5668\u6765\u68c0\u6d4b\u9875\u9762\u8df3\u8f6c\u3002\u5f53\u9875\u9762\u5378\u8f7d\u65f6\uff0c<\/p>\n<p> \u5143\u7d20\u5c06\u4ece DOM \u4e2d\u5220\u9664\uff0c\u8fd9\u5c06\u89e6\u53d1\u89c2\u5bdf\u5668\u7684 removedNodes \u56de\u8c03\u3002 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>const observer = new MutationObserver(function(mutations) {\n  for (let mutation of mutations) {\n    if (mutation.removedNodes.length &gt; 0) {\n      \/\/ \u9875\u9762\u5df2\u5378\u8f7d\n    }\n  }\n});\n\nobserver.observe(document.body, { childList: true });<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5224\u65ad\u9875\u9762\u8df3\u8f6c\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>javascript \u63d0\u4f9b\u56db\u79cd\u65b9\u6cd5\u68c0\u6d4b\u9875\u9762\u8df3\u8f6c\uff1awindow.onbeforeunload \u4e8b\u4ef6\uff1a\u5728\u9875\u9762\u5378\u8f7d\u524d\u89e6\u53d1\u3002window.onunload \u4e8b\u4ef6\uff1a\u5728\u9875\u9762\u5b9e\u9645\u5378\u8f7d\u540e\u89e6\u53d1\u3002history api \u7684 popstate \u4e8b\u4ef6\u76d1\u542c\u5668\uff1a\u68c0\u6d4b\u5386\u53f2\u8bb0\u5f55\u53d8\u5316\u3002mutationobserver\uff1a\u76d1\u542c dom \u66f4\u6539\uff0c\u68c0\u6d4b\u9875\u9762\u5378\u8f7d\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u5224\u65ad\u9875\u9762\u8df3\u8f6c JavaScript \u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u68c0\u6d4b\u9875\u9762\u8df3\u8f6c\uff0c\u5305\u62ec\uff1a 1. window.onbeforeunload \u4e8b\u4ef6 \u6b64\u4e8b\u4ef6\u5728\u6d4f\u89c8\u5668\u68c0\u6d4b\u5230\u9875\u9762\u5373\u5c06\u5378\u8f7d\u65f6\u89e6\u53d1\u3002\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u6355\u83b7\u6b64\u4e8b\u4ef6\uff0c\u5e76\u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u524d\u6267\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a window.onbeforeunload = function(e) { \/\/ \u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u524d\u6267\u884c\u64cd\u4f5c }; \u767b\u5f55\u540e\u590d\u5236 2. window.onunload \u4e8b\u4ef6 \u6b64\u4e8b\u4ef6\u5728\u9875\u9762\u5b9e\u9645\u5378\u8f7d\u65f6\u89e6\u53d1\u3002\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u6355\u83b7\u6b64\u4e8b\u4ef6\uff0c\u5e76\u6267\u884c\u9875\u9762\u8df3\u8f6c\u540e\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a window.onunload = function(e) { \/\/ \u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u540e\u6267\u884c\u64cd\u4f5c }; \u767b\u5f55\u540e\u590d\u5236 3. history API JavaScript \u7684\u5386\u53f2\u8bb0\u5f55 API \u63d0\u4f9b\u4e86 pushState() \u548c replaceState() \u65b9\u6cd5\uff0c\u5141\u8bb8\u60a8\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u66f4\u65b0\u6d4f\u89c8\u5668\u7684\u5386\u53f2\u8bb0\u5f55\u3002\u5f53\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u4e0d\u4f1a\u89e6\u53d1 window.onbeforeunload \u6216 window.onunload \u4e8b\u4ef6\u3002\u8981\u68c0\u6d4b\u5386\u53f2\u8bb0\u5f55\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528 popstate [&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-3210","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3210","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=3210"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3210\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}