{"id":5063,"date":"2016-06-01T18:29:40","date_gmt":"2016-06-01T10:29:40","guid":{"rendered":"https:\/\/sdeno.com\/?p=5063"},"modified":"2016-06-01T18:29:40","modified_gmt":"2016-06-01T10:29:40","slug":"%e8%b7%a8%e6%b5%8f%e8%a7%88%e5%99%a8-html5-postmessage-%e6%96%b9%e6%b3%95%e4%bb%a5%e5%8f%8a-message-%e4%ba%8b%e4%bb%b6%e6%a8%a1%e6%8b%9f%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5063","title":{"rendered":"\u8de8\u6d4f\u89c8\u5668 HTML5 postMessage \u65b9\u6cd5\u4ee5\u53ca message \u4e8b\u4ef6\u6a21\u62df\u5b9e\u73b0"},"content":{"rendered":"<p style=\"color: #000000;\">postMessage \u662f HTML5 \u65b0\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8de8\u57df\u7a97\u53e3\u4e4b\u95f4\u901a\u8baf\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u53ea\u6709 IE8+, Firefox 3, Opera 9, Chrome 3\u548c Safari 4 \u652f\u6301\uff0c\u800c\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u8ff0 postMessage \u65b9\u6cd5\u4e0e message \u4e8b\u4ef6\u8de8\u6d4f\u89c8\u5668\u5b9e\u73b0\u3002<\/p>\n<p>postMessage \u65b9\u6cd5 JSONP \u6280\u672f\u4e0d\u4e00\u6837\uff0c\u524d\u8005\u662f\u524d\u7aef\u64c5\u957f\u8de8\u57df\u6587\u6863\u6570\u636e\u5373\u65f6\u901a\u8baf\uff0c\u540e\u8005\u64c5\u957f\u9488\u5bf9\u8de8\u57df\u670d\u52a1\u7aef\u6570\u636e\u901a\u8baf\uff0cpostMessage \u5e94\u7528\u573a\u666f\u80fd\u8bf4\u660e\u8fd9\u4e2a\u533a\u522b\uff1a<\/p>\n<h2 style=\"color: #000000;\">\u5e94\u7528\u573a\u666f\u4e3e\u4f8b<\/h2>\n<ol style=\"color: #000000;\">\n<li>webOS \u4f7f\u7528 iframe \u5d4c\u5165\u7b2c\u4e09\u65b9\u5e94\u7528\uff0c\u6b64\u65f6 webOS \u4e0e\u5e94\u7528\u9700\u8981\u5b9e\u65f6\u63a5\u6536\/\u53d1\u9001\u5404\u81ea\u7684\u6d88\u606f\u4e0e\u54cd\u5e94\u4e8b\u4ef6\u3002<\/li>\n<li>\u9875\u9762\u5f39\u51fa\u4e00\u4e2a\u7531 iframe \u5c42\uff0c\u5d4c\u5165\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\u56fe\u7247\u4e0a\u4f20\u9875\u9762\uff0c\u6587\u4ef6\u4e0a\u4f20\u5b8c\u6bd5\u540e\u9700\u8981\u83b7\u53d6\u8fd4\u56de\u56fe\u7247\u5730\u5740\u63d2\u5165\u5230\u7f16\u8f91\u5668\u3002<\/li>\n<li>iframe \u8de8\u57df\u9ad8\u5ea6\u81ea\u9002\u5e94\u3002<a style=\"color: #108ac6;\" href=\"http:\/\/www.iteye.com\/topic\/1132907\" target=\"_blank\">\u53ef\u4ee5500%\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u7684\u524d\u7aefUI\u6846\u67b6\uff01<\/a><\/li>\n<\/ol>\n<h2 style=\"color: #000000;\">HTML5 postMessage \u65b9\u6cd5<\/h2>\n<p style=\"color: #000000;\">postMessage \u53ef\u4ee5\u5b9e\u73b0\u8de8\u57df\u6587\u6863\u7684\u6d88\u606f\u4f20\u8f93\uff08Cross Document Messaging\uff09\u3002<\/p>\n<p style=\"color: #000000;\">\u5411\u5916\u754c\u7a97\u53e3\u53d1\u9001\u6d88\u606f\uff1a<\/p>\n<div id=\"\" class=\"dp-highlighter\" style=\"color: #000000;\">\n<div class=\"bar\">\n<div class=\"tools\" style=\"font-weight: bold; color: black;\">.\u4ee3\u7801\u00a0<embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/www.iteye.com\/javascripts\/syntaxhighlighter\/clipboard_new.swf\" wmode=\"transparent\" flashvars=\"clipboard=otherWindow.postMessage(message%2C%20targetOrigin)%3B\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\"><\/embed>\u00a0<a style=\"color: #006699;\" title=\"\u6536\u85cf\u8fd9\u6bb5\u4ee3\u7801\"><figure class=\"mdx-lazyload-container\" style=\"\"><div style=\"padding-top:50%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img class=\"star lazyload\" title=\"\u6536\u85cf\u4ee3\u7801\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"http:\/\/www.iteye.com\/images\/icon_star.png\" alt=\"\u6536\u85cf\u4ee3\u7801\"><\/figure><\/a><\/div>\n<\/div>\n<ol class=\"dp-default\" style=\"color: #2b91af;\" start=\"1\">\n<li><span style=\"color: black;\"><span style=\"color: black;\">otherWindow.postMessage(message,\u00a0targetOrigin);\u00a0\u00a0<\/span><\/span><\/li>\n<\/ol>\n<\/div>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\"><em style=\"font-style: italic;\">otherWindow:\u00a0<\/em>\u6307\u76ee\u6807\u7a97\u53e3\uff0c\u662f window.frames \u5c5e\u6027\u7684\u6210\u5458\u6216\u8005\u7531 window.open \u65b9\u6cd5\u521b\u5efa\u7684\u7a97\u53e3<\/p>\n<p style=\"color: #000000;\">\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul style=\"color: #000000;\">\n<li><em style=\"font-style: italic;\">message:<\/em>\u662f\u8981\u53d1\u9001\u7684\u6d88\u606f\uff0c\u7c7b\u578b\u4e3a String\u3001Object (IE8\u30019 \u4e0d\u652f\u6301)<\/li>\n<li><em style=\"font-style: italic;\">targetOrigin:<\/em>\u662f\u9650\u5b9a\u6d88\u606f\u63a5\u6536\u8303\u56f4\uff0c\u4e0d\u9650\u5236\u8bf7\u4f7f\u7528 \u2018*\u2019<\/li>\n<\/ul>\n<h2 style=\"color: #000000;\">HTML5 message \u4e8b\u4ef6<\/h2>\n<p style=\"color: #000000;\">\u7ed1\u5b9a\u6d88\u606f\u4e8b\u4ef6\uff1a<\/p>\n<div id=\"\" class=\"dp-highlighter\" style=\"color: #000000;\">\n<div class=\"bar\">\n<div class=\"tools\" style=\"font-weight: bold; color: black;\">.\u4ee3\u7801\u00a0<embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/www.iteye.com\/javascripts\/syntaxhighlighter\/clipboard_new.swf\" wmode=\"transparent\" flashvars=\"clipboard=window.addEventListener('message'%2C%20receiver%2C%20false)%3B%20%0Afunction%20receiver%20(event)%20%7B%20%0A%20%20if%20(event.origin%20%3D%3D%3D%20'http%3A%2F%2Fexample.com')%20%7B%20%0A%20%20%20%20if%20(event.data%20%3D%3D%3D%20'Hello%20world')%20%7B%20%0A%20%20%20%20%20%20event.source.postMessage('Hello'%2C%20event.origin)%3B%20%0A%20%20%20%20%7D%20else%20%7B%20%0A%20%20%20%20%20%20alert(event.data)%3B%20%0A%20%20%20%20%7D%3B%20%0A%20%20%7D%3B%20%0A%7D%3B\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\"><\/embed>\u00a0<a style=\"color: #006699;\" title=\"\u6536\u85cf\u8fd9\u6bb5\u4ee3\u7801\"><figure class=\"mdx-lazyload-container\" style=\"\"><div style=\"padding-top:50%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img class=\"star lazyload\" title=\"\u6536\u85cf\u4ee3\u7801\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"http:\/\/www.iteye.com\/images\/icon_star.png\" alt=\"\u6536\u85cf\u4ee3\u7801\"><\/figure><\/a><\/div>\n<\/div>\n<ol class=\"dp-default\" style=\"color: #2b91af;\" start=\"1\">\n<li><span style=\"color: black;\"><span style=\"color: black;\">window.addEventListener(<\/span><span class=\"string\" style=\"color: blue;\">&#8216;message&#8217;<\/span><span style=\"color: black;\">,\u00a0receiver,\u00a0false);\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">function\u00a0receiver\u00a0(event)\u00a0{\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0if\u00a0(event.origin\u00a0===\u00a0<span class=\"string\" style=\"color: blue;\">&#8216;http:\/\/example.com&#8217;<\/span><span style=\"color: black;\">)\u00a0{\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0if\u00a0(event.data\u00a0===\u00a0<span class=\"string\" style=\"color: blue;\">&#8216;Hello\u00a0world&#8217;<\/span><span style=\"color: black;\">)\u00a0{\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0event.source.postMessage(<span class=\"string\" style=\"color: blue;\">&#8216;Hello&#8217;<\/span><span style=\"color: black;\">,\u00a0event.origin);\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0alert(event.data);\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0};\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0};\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">};\u00a0\u00a0<\/span><\/li>\n<\/ol>\n<\/div>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">\u8c03\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u63a5\u6536 Event \u5bf9\u8c61\uff0c\u6709\u4e09\u4e2a\u5e38\u7528\u5c5e\u6027\uff1a<\/p>\n<ul style=\"color: #000000;\">\n<li><em style=\"font-style: italic;\">data:<\/em>\u6d88\u606f<\/li>\n<li><em style=\"font-style: italic;\">origin:<\/em>\u6d88\u606f\u6765\u6e90\u5730\u5740<\/li>\n<li><em style=\"font-style: italic;\">source:<\/em>\u6e90 DOMWindow \u5bf9\u8c61<a style=\"color: #108ac6;\" href=\"http:\/\/www.iteye.com\/topic\/1132907\" target=\"_blank\">\u53ef\u4ee5500%\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u7684\u524d\u7aefUI\u6846\u67b6\uff01<\/a><\/li>\n<\/ul>\n<h2 style=\"color: #000000;\">message \u4e8b\u4ef6\u5728\u4f4e\u7248\u672c\u6d4f\u89c8\u5668\u4e0b\u6a21\u62df\u5b9e\u73b0<\/h2>\n<p style=\"color: #000000;\">\u5bf9\u4e8e\u652f\u6301 postMessage \u65b9\u6cd5\u7684\u6d4f\u89c8\u5668\u76f4\u63a5\u4f7f\u7528\u5b83\uff1b\u800c\u5bf9\u4e8e IE6\u30017 \u91c7\u7528\u4e86\u6bd4\u8f83\u6210\u719f\u7684 window.name \u4fdd\u5b58\u6570\u636e\u4ee5\u53ca\u8de8\u57df iframe \u9759\u6001\u4ee3\u7406\u52a8\u6001\u4f20\u8f93\u65b9\u6848\uff0c\u4e0b\u9762\u7b80\u79f0 Cross Frame\u3002<\/p>\n<p style=\"color: #000000;\"><strong style=\"font-weight: bold;\">Cross Frame<\/strong><\/p>\n<p style=\"color: #000000;\">\u5047\u8bbe\u5728\u57df www.a.com \u4e0a\u6709\u9875\u9762 a.html \u548c\u4ee3\u7406\u9875\u9762 proxy-a.html \uff0c \u53e6\u4e00\u4e2a\u57df www.b.com \u4e0a\u6709\u4e2a\u9875\u9762 b.html \u548c\u4ee3\u7406\u9875\u9762 proxy-b.html\uff0ca.html \u9700\u8981\u5411 b.html \u4e2d\u53d1\u9001\u6d88\u606f\u65f6\uff0c\u9875\u9762\u4f1a\u521b\u5efa\u4e00\u4e2a\u9690\u85cf\u7684 iframe \u6307\u5411 proxy-b.html \uff0c\u5e76\u628a\u6d88\u606f\u8d4b\u4e88 iframe.name \u5c5e\u6027\uff0c\u6b64\u65f6 proxy-b.html \u53ef\u4ee5\u901a\u8fc7 window.name \u83b7\u53d6\u5230\u6d88\u606f\uff0c\u7531\u4e8e proxy-b.html \u4e0e b.html \u662f\u540c\u57df\uff0cproxy-b.html \u53ef\u4ee5\u628a\u6d88\u606f\u8d4b\u4e88 b.html\u3002 b.html \u8981\u7ed9 a.html \u53d1\u9001\u6d88\u606f\u65f6\uff0c\u539f\u7406\u4e00\u6837\u3002<\/p>\n<p style=\"color: #000000;\"><strong style=\"font-weight: bold;\">\u81ea\u52a8\u6355\u83b7\u4ee3\u7406 URL<\/strong><\/p>\n<p style=\"color: #000000;\">\u5728 Cross Frame \u65b9\u6848\u4e2d\uff0c\u901a\u4fe1\u53cc\u65b9\u5fc5\u987b\u786e\u5207\u7684\u77e5\u9053\u9759\u6001\u4ee3\u7406\u6587\u4ef6\u7684 URL\uff0c\u663e\u7136\u8fd9\u4e2a\u6781\u5927\u7684\u9650\u5236\u4e86\u5e94\u7528\u8303\u56f4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7ea6\u5b9a\u6539\u5584\uff1a\u9759\u6001\u4ee3\u7406\u6587\u4ef6\u5fc5\u987b\u7f6e\u4e8e\u901a\u4fe1\u9875\u9762\u6240\u5728\u57df\u6839\u76ee\u5f55\uff0c\u4e14\u6587\u4ef6\u540d\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\uff0c\u5982 messageEvent-proxy.html\u3002<\/p>\n<p style=\"color: #000000;\">\u6709\u4e86\u4e0a\u8ff0\u7ea6\u5b9a\uff0c\u63a5\u4e0b\u6765\u53ef\u4ee5\u7528\u4e00\u4e9b\u5de7\u5999\u7684\u65b9\u6cd5\u8ba9\u53cc\u65b9\u81ea\u52a8\u6355\u83b7\u4ee3\u7406 URL\u3002\u4ee5 http:\/\/www.a.com\/a.html \u901a\u8fc7 iframe \u5d4c\u5165 http:\/\/www.b.com\/b.html \u4fdd\u6301\u6570\u636e\u4ea4\u6362\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n<p style=\"color: #000000;\">b.html \u7684\u9759\u6001\u4ee3\u7406\u8def\u5f84\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u5206\u6790 iframe.src \u540e\u5f97\u77e5\uff1b\u800c\u4ece\u6846\u67b6 b.html \u5185\u83b7\u53d6\u7236\u9875\u9762\u5c31\u6bd4\u8f83\u9ebb\u70e6\u4e86\uff0c\u56e0\u4e3a\u8de8\u57df\u540e\u7684 parent.location.href \u5c5e\u6027\u53ea\u53ef\u5199\u5165\u4e0d\u53ef\u8bfb\u53d6\uff0c\u4e0d\u8fc7\u8fd8\u53ef\u4ee5\u501f\u7528 document.referrer \u5c5e\u6027\u6765\u5206\u6790\u6765\u8def\u5730\u5740\u5f97\u77e5\u7236\u9875\u9762 url\u3002document.referrer \u662f\u4e00\u4e2a\u4e0d\u7a33\u5b9a\u7684\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528 iframe \u4e2d window.name \u5237\u65b0\u4e5f\u4e0d\u4f1a\u53d8\u5316\u7684\u7279\u6027\uff0c\u7528\u6b64\u6765\u4fdd\u5b58\u7236\u9875\u9762 a.html \u7684\u5730\u5740\u3002<\/p>\n<p style=\"color: #000000;\"><strong style=\"font-weight: bold;\">\u6301\u7eed\u8ddf\u8e2a URL<\/strong><a style=\"color: #108ac6;\" href=\"http:\/\/www.iteye.com\/topic\/1132907\" target=\"_blank\">\u53ef\u4ee5500%\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u7684\u524d\u7aefUI\u6846\u67b6\uff01<\/a><\/p>\n<p style=\"color: #000000;\">a.html \u7b2c\u4e00\u6b21\u901a\u8fc7\u63d0\u53d6 iframe.src \u8def\u5f84\u53ef\u5f97\u77e5 b.html \u7684\u5730\u5740\uff0c\u5047\u82e5 b.html \u8df3\u8f6c\u5230\u5176\u4ed6\u57df\u540d\u7684\u65f6\u5019\uff0c\u6b64\u65f6\u5c31\u4f1a\u5931\u53bb\u5bf9 iframe \u5185\u9759\u6001\u4ee3\u7406\u7684\u8054\u7edc\u3002 \u597d\u5728\u65b0\u9875\u9762\u7531\u4e8e\u80fd\u591f\u83b7\u53d6\u7236\u9875\u9762 a.html \u4fdd\u5b58\u5728 window.name \u7684\u9759\u6001\u4ee3\u7406\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5728\u65b0\u9875\u9762\u521d\u59cb\u5316\u7684\u65f6\u5019\u5411 a.html \u4f20\u9012\u6d88\u606f\u544a\u8bc9\u5b83\u65b0\u7684\u5730\u5740\uff0c\u8fd9\u6837\u5c31\u80fd\u6301\u7eed\u8ddf\u8e2a iframe \u4e2d\u7684 URL\u3002<\/p>\n<h2 style=\"color: #000000;\">\u5f00\u6e90\u4e8b\u4ef6\u5e93 messageEvent.js<\/h2>\n<p style=\"color: #000000;\">\u201cmessageEvent.js\u201d\u662f\u9488\u5bf9\u4e0a\u8ff0\u65b9\u6848\u5c01\u88c5\u7684 message \u4e8b\u4ef6\u4e0e postMessage \u65b9\u6cd5\u5e93\uff0c\u5b83\u8ba9\u5404\u4e2a\u6d4f\u89c8\u5668\u4e4b\u95f4 message \u7684 Event \u5bf9\u8c61\u6210\u5458\u5c5e\u6027\u7edf\u4e00\uff0cevent.data \u5c5e\u6027\u80fd\u4f20\u9012\u591a\u8fbe 2MB \u7684\u6587\u672c\u4fe1\u606f\uff0c\u5e76\u4e14\u80fd\u8ba9 IE6-9 \u6d4f\u89c8\u5668\u50cf\u5176\u4ed6\u73b0\u4ee3\u6d4f\u89c8\u4e00\u6837\u652f\u6301 Object \u7c7b\u578b\u6570\u636e\u8fdb\u884c\u4f20\u9012 \uff08\u5185\u90e8\u4f7f\u7528\u6df1\u62f7\u8d1d\u65b9\u5f0f\uff09\u3002<\/p>\n<p style=\"color: #000000;\">\u82e5\u5e94\u7528\u53cc\u65b9\u9875\u9762\u90fd\u91c7\u7528 messageEvent.js\uff0c\u5373\u53ef\u8f7b\u677e\u5b9e\u73b0\u8de8\u57df\u901a\u4fe1\u3002<\/p>\n<p style=\"color: #000000;\"><strong style=\"font-weight: bold;\">\u63a5\u53e3<\/strong><\/p>\n<ul style=\"color: #000000;\">\n<li><em style=\"font-style: italic;\">add(callback)<\/em>\u6dfb\u52a0 message \u4e8b\u4ef6<\/li>\n<li><em style=\"font-style: italic;\">remove(callback)<\/em>\u5378\u8f7d message \u4e8b\u4ef6<\/li>\n<li><em style=\"font-style: italic;\">postMessage(otherWindow, message, targetOrigin)<\/em>\u5411\u5916\u90e8\u7a97\u53e3\u53d1\u9001\u6d88\u606f<\/li>\n<\/ul>\n<p style=\"color: #000000;\"><strong style=\"font-weight: bold;\">\u901a\u8fc7 jQuery \u4f7f\u7528\u5b83<\/strong><\/p>\n<p style=\"color: #000000;\">jQuery \u662f\u4e00\u4e2a\u5e94\u7528\u6bd4\u8f83\u5e7f\u6cdb\u7684 DOM \u5e93\uff0c\u5b83\u7684\u4e8b\u4ef6\u673a\u5236\u975e\u5e38\u5f3a\u5927\u800c\u7cbe\u5999\uff0c\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u3002\u82e5\u9875\u9762\u5f15\u7528\u4e86 jQuery\uff0c messageEvent.js \u4f1a\u4e3a\u4e3a\u5b83\u63d0\u4f9b\u652f\u6301\uff0c\u4f60\u53ef\u4ee5\u7528\u719f\u6089\u7684jQuery api \u98ce\u683c\u7f16\u7a0b\uff0c\u5982\uff1a\u00a0<a style=\"color: #108ac6;\" href=\"http:\/\/www.iteye.com\/topic\/1132907\" target=\"_blank\">\u53ef\u4ee5500%\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u7684\u524d\u7aefUI\u6846\u67b6\uff01<\/a><\/p>\n<div id=\"\" class=\"dp-highlighter\" style=\"color: #000000;\">\n<div class=\"bar\">\n<div class=\"tools\" style=\"font-weight: bold; color: black;\">.\u4ee3\u7801\u00a0<embed width=\"14\" height=\"15\" type=\"application\/x-shockwave-flash\" src=\"http:\/\/www.iteye.com\/javascripts\/syntaxhighlighter\/clipboard_new.swf\" wmode=\"transparent\" flashvars=\"clipboard=jQuery(window).bind('message'%2C%20function%20(event)%20%7B%20%0A%20%20%20%20%20%20alert(event.data)%20%0A%7D)%3B%20%0A%20%20%20%0AjQuery(window).message(function%20(event)%20%7B%20%0A%20%20%20%20%20%20alert(event.data)%20%0A%7D)%3B%20%0A%20%20%20%0AjQuery.postMessage(iframe.contentWindow%2C%20'hello%20world'%2C%20'*')%3B%20%0A%20%20%20%0AjQuery(window).unbind('message')%3B\" quality=\"high\" allowscriptaccess=\"always\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\"><\/embed>\u00a0<a style=\"color: #006699;\" title=\"\u6536\u85cf\u8fd9\u6bb5\u4ee3\u7801\"><figure class=\"mdx-lazyload-container\" style=\"\"><div style=\"padding-top:50%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img class=\"star lazyload\" title=\"\u6536\u85cf\u4ee3\u7801\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"http:\/\/www.iteye.com\/images\/icon_star.png\" alt=\"\u6536\u85cf\u4ee3\u7801\"><\/figure><\/a><\/div>\n<\/div>\n<ol class=\"dp-default\" style=\"color: #2b91af;\" start=\"1\">\n<li><span style=\"color: black;\"><span style=\"color: black;\">jQuery(window).bind(<\/span><span class=\"string\" style=\"color: blue;\">&#8216;message&#8217;<\/span><span style=\"color: black;\">,\u00a0function\u00a0(event)\u00a0{\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0alert(event.data)\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">});\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">jQuery(window).message(function\u00a0(event)\u00a0{\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0alert(event.data)\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">});\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">jQuery.postMessage(iframe.contentWindow,\u00a0<span class=\"string\" style=\"color: blue;\">&#8216;hello\u00a0world&#8217;<\/span><span style=\"color: black;\">,\u00a0<\/span><span class=\"string\" style=\"color: blue;\">&#8216;*&#8217;<\/span><span style=\"color: black;\">);\u00a0\u00a0\u00a0<\/span><\/span><\/li>\n<li><span style=\"color: black;\">\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/li>\n<li><span style=\"color: black;\">jQuery(window).unbind(<span class=\"string\" style=\"color: blue;\">&#8216;message&#8217;<\/span><span style=\"color: black;\">);\u00a0\u00a0<\/span><\/span><\/li>\n<\/ol>\n<\/div>\n<p style=\"color: #000000;\">\n<p style=\"color: #000000;\">\u7531\u4e8e jQuery \u628a\u5305\u88c5\u540e\u7684 Event \u5bf9\u8c61\u7528 data \u5c5e\u6027\u6765\u4fdd\u5b58 bind \u65b9\u6cd5\u4f20\u5165\u7684\u989d\u5916\u6570\u636e\uff0c\u5bfc\u81f4\u4e0e message \u4e8b\u4ef6\u81ea\u8eab\u7684 event.data \u5c5e\u6027\u51b2\u7a81\u2014\u2014\u8fd9\u662f\u4e00\u4e2a\u8bbe\u8ba1\u9519\u8bef\u3002\u4e3a\u4e86\u8ba9 message \u4e8b\u4ef6\u80fd\u591f\u6b63\u786e\u83b7\u53d6 event.data\uff0cmessageEvent.js \u901a\u8fc7\u64cd\u4f5c jQuery \u5e95\u5c42\u7f13\u5b58\u5f3a\u5236\u8986\u76d6\u4e86 bind \u65b9\u6cd5\u4f20\u5165\u7684\u9644\u52a0\u6570\u636e (\u53ea\u9488\u5bf9 message \u7c7b\u578b)\u3002\u5f53\u7136\uff0c\u6211\u4ecd\u7136\u671f\u5f85 jQuery \u672a\u6765\u7248\u672c\u80fd\u591f\u53d6\u6d88\u6389 bind \u65b9\u6cd5\u7684\u9e21\u808b\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>postMessage \u662f HTML5 \u65b0\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8de8\u57df\u7a97\u53e3\u4e4b\u95f4\u901a\u8baf\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u53ea\u6709 IE8+, Fi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-5063","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5063"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5063\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}