我有两个 HTML 文件。在一个 HTML 文件中,我使用postMessage
HTML5 发布了一条消息。如何在加载时在另一个 HTML 文件中获取发布的消息?
一.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script language="javascript">
$(document).ready(function() {
//post message
var iframeWin = document.getElementById("iframe_id").contentWindow;
iframeWin.postMessage("helloooo");
});
</script>
<title>IFrame Example</title>
</head>
<body>
<input id="myInput" type="hidden" value="IDDUSER">
<iframe name="iframe" id="iframe_id" src="Two.html" height="150"></iframe>
</body>
</html>
二.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script language="javascript">
$(document).ready(function() {
//Get posted message here
});
</script>
<title>IFrame Child Example</title>
</head>
<body>
<p id="received-message">I've heard nothing yet</p>
<h1>Iframe</h1>
</body>
</html>