{"id":3658,"date":"2024-11-10T12:49:44","date_gmt":"2024-11-10T04:49:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3658\/"},"modified":"2024-11-10T12:49:44","modified_gmt":"2024-11-10T04:49:44","slug":"chrome-%e4%b8%ad-onbeforeunload-%e4%ba%8b%e4%bb%b6%e5%a4%b1%e6%95%88%e4%ba%86%ef%bc%8c%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%a6%bb%e5%bc%80%e9%a1%b5%e9%9d%a2%e6%8f%90%e7%a4%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3658\/","title":{"rendered":"Chrome \u4e2d onbeforeunload \u4e8b\u4ef6\u5931\u6548\u4e86\uff0c\u5982\u4f55\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111207086919.jpg\" class=\"aligncenter\" title=\"Chrome \u4e2d onbeforeunload \u4e8b\u4ef6\u5931\u6548\u4e86\uff0c\u5982\u4f55\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\uff1f\u63d2\u56fe\" alt=\"Chrome \u4e2d onbeforeunload \u4e8b\u4ef6\u5931\u6548\u4e86\uff0c\u5982\u4f55\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>chrome \u4e2d window.onbeforeunload \u4e8b\u4ef6\u5931\u6548\u95ee\u9898\u4e0e\u66ff\u4ee3\u65b9\u6848<\/strong><\/p>\n<p>\u7531\u4e8e\u5b89\u5168\u653f\u7b56\u7684\u66f4\u65b0\uff0c\u4ece chrome 51 \u7248\u672c\u8d77\uff0conbeforeunload \u4e8b\u4ef6\u5df2\u88ab\u5f03\u7528\uff0c\u4ee5\u9632\u6b62\u5f39\u51fa\u7a97\u53e3\u7684\u6ee5\u7528\u548c\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad\u3002\u56e0\u6b64\uff0c\u5728\u6700\u65b0\u7248\u672c\u7684 chrome \u6d4f\u89c8\u5668\u4e2d\uff0c\u5173\u95ed\u6d4f\u89c8\u5668\u65f6 onbeforeunload \u4e8b\u4ef6\u4e0d\u4f1a\u518d\u89e6\u53d1\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u65b9\u6848<\/strong><\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6848\uff1a<\/p>\n<p><strong>1. \u9875\u9762\u53ef\u89c1\u6027 api<\/strong><\/p>\n<p>\u9875\u9762\u53ef\u89c1\u6027 api \u63d0\u4f9b\u4e86 visibilitychange \u4e8b\u4ef6\uff0c\u53ef\u5728\u9875\u9762\u53ef\u89c1\u6027\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\u3002\u5f53\u9875\u9762\u53d8\u4e3a\u9690\u85cf\u6216\u5373\u5c06\u5378\u8f7d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u4e8b\u4ef6\u6765\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002<\/p>\n<p><strong>2. \u6d4f\u89c8\u5668\u4f1a\u8bdd\u5b58\u50a8<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728\u6d4f\u89c8\u5668\u4f1a\u8bdd\u5b58\u50a8\uff08\u4f8b\u5982 \uff09\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\uff0c\u6765\u5224\u65ad\u7528\u6237\u662f\u5426\u8bbf\u95ee\u8fc7\u8be5\u9875\u9762\u3002\u5728\u9875\u9762\u5378\u8f7d\u65f6\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u8be5\u6807\u5fd7\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002<\/p>\n<p><strong>3. \u8ba1\u65f6\u5668\u68c0\u67e5<\/strong><\/p>\n<p>\u5b9a\u671f\u4f7f\u7528\u8ba1\u65f6\u5668\u68c0\u67e5\u7528\u6237\u4e0e\u9875\u9762\u7684\u4ea4\u4e92\u60c5\u51b5\uff0c\u5982\u679c\u4e00\u6bb5\u65f6\u95f4\u5185\u672a\u68c0\u6d4b\u5230\u4efb\u4f55\u4ea4\u4e92\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002<\/p>\n<p><strong>4. dom \u4fee\u6539\u68c0\u6d4b<\/strong><\/p>\n<p>\u76d1\u89c6\u6587\u6863\u7684 dom \u7ed3\u6784\u7684\u53d8\u5316\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u7528\u6237\u4fee\u6539\u4e86\u9875\u9762\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u9875\u9762\u53ef\u89c1\u6027 api \u7684\u66ff\u4ee3\u65b9\u6848\uff1a<\/p>\n<pre>document.addEventListener('visibilitychange', function() {\n  if (document.visibilityState === 'hidden') {\n    \/\/ \u663e\u793a\u79bb\u5f00\u63d0\u793a\n  }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u8fd9\u4e9b\u66ff\u4ee3\u65b9\u6848\u53ef\u80fd\u4f1a\u53d7\u5230\u6d4f\u89c8\u5668\u7684\u5b89\u5168\u9650\u5236\u6216\u7528\u6237\u7684\u9690\u79c1\u8bbe\u7f6e\u7684\u5f71\u54cd\u3002\u5efa\u8bae\u5728\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6848\u4e4b\u524d\uff0c\u8003\u8651\u5176\u5b89\u5168\u6027\u548c\u9690\u79c1\u5f71\u54cd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fChrome \u4e2d onbeforeunload \u4e8b\u4ef6\u5931\u6548\u4e86\uff0c\u5982\u4f55\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\uff1f\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>chrome \u4e2d window.onbeforeunload \u4e8b\u4ef6\u5931\u6548\u95ee\u9898\u4e0e\u66ff\u4ee3\u65b9\u6848 \u7531\u4e8e\u5b89\u5168\u653f\u7b56\u7684\u66f4\u65b0\uff0c\u4ece chrome 51 \u7248\u672c\u8d77\uff0conbeforeunload \u4e8b\u4ef6\u5df2\u88ab\u5f03\u7528\uff0c\u4ee5\u9632\u6b62\u5f39\u51fa\u7a97\u53e3\u7684\u6ee5\u7528\u548c\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad\u3002\u56e0\u6b64\uff0c\u5728\u6700\u65b0\u7248\u672c\u7684 chrome \u6d4f\u89c8\u5668\u4e2d\uff0c\u5173\u95ed\u6d4f\u89c8\u5668\u65f6 onbeforeunload \u4e8b\u4ef6\u4e0d\u4f1a\u518d\u89e6\u53d1\u3002 \u66ff\u4ee3\u65b9\u6848 \u4e3a\u4e86\u5b9e\u73b0\u79bb\u5f00\u9875\u9762\u63d0\u793a\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6848\uff1a 1. \u9875\u9762\u53ef\u89c1\u6027 api \u9875\u9762\u53ef\u89c1\u6027 api \u63d0\u4f9b\u4e86 visibilitychange \u4e8b\u4ef6\uff0c\u53ef\u5728\u9875\u9762\u53ef\u89c1\u6027\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\u3002\u5f53\u9875\u9762\u53d8\u4e3a\u9690\u85cf\u6216\u5373\u5c06\u5378\u8f7d\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u4e8b\u4ef6\u6765\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002 2. \u6d4f\u89c8\u5668\u4f1a\u8bdd\u5b58\u50a8 \u53ef\u4ee5\u901a\u8fc7\u5728\u6d4f\u89c8\u5668\u4f1a\u8bdd\u5b58\u50a8\uff08\u4f8b\u5982 \uff09\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\uff0c\u6765\u5224\u65ad\u7528\u6237\u662f\u5426\u8bbf\u95ee\u8fc7\u8be5\u9875\u9762\u3002\u5728\u9875\u9762\u5378\u8f7d\u65f6\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u8be5\u6807\u5fd7\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002 3. \u8ba1\u65f6\u5668\u68c0\u67e5 \u5b9a\u671f\u4f7f\u7528\u8ba1\u65f6\u5668\u68c0\u67e5\u7528\u6237\u4e0e\u9875\u9762\u7684\u4ea4\u4e92\u60c5\u51b5\uff0c\u5982\u679c\u4e00\u6bb5\u65f6\u95f4\u5185\u672a\u68c0\u6d4b\u5230\u4efb\u4f55\u4ea4\u4e92\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002 4. dom \u4fee\u6539\u68c0\u6d4b \u76d1\u89c6\u6587\u6863\u7684 dom \u7ed3\u6784\u7684\u53d8\u5316\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u7528\u6237\u4fee\u6539\u4e86\u9875\u9762\uff0c\u5219\u53ef\u4ee5\u663e\u793a\u79bb\u5f00\u63d0\u793a\u3002 \u4ee3\u7801\u793a\u4f8b \u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u9875\u9762\u53ef\u89c1\u6027 api \u7684\u66ff\u4ee3\u65b9\u6848\uff1a document.addEventListener(&#8216;visibilitychange&#8217;, function() { if (document.visibilityState === &#8216;hidden&#8217;) { \/\/ \u663e\u793a\u79bb\u5f00\u63d0\u793a } }); \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a\u8fd9\u4e9b\u66ff\u4ee3\u65b9\u6848\u53ef\u80fd\u4f1a\u53d7\u5230\u6d4f\u89c8\u5668\u7684\u5b89\u5168\u9650\u5236\u6216\u7528\u6237\u7684\u9690\u79c1\u8bbe\u7f6e\u7684\u5f71\u54cd\u3002\u5efa\u8bae\u5728\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6848\u4e4b\u524d\uff0c\u8003\u8651\u5176\u5b89\u5168\u6027\u548c\u9690\u79c1\u5f71\u54cd\u3002 \u4ee5\u4e0a\u5c31\u662fChrome [&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-3658","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3658","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=3658"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3658\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}