一个Web项目,父页面给子页面传递参数,代码如下:
1 <!DOCTYPE html> 2 <html lang="en-us"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>Unity WebGL Player | JsToUnityToDBDemo</title> 7 <link rel="shortcut icon" href="TemplateData/favicon.ico"> 8 <link rel="stylesheet" href="TemplateData/style.css"> 9 <script src="TemplateData/UnityProgress.js"></script> 10 <script src="Build/UnityLoader.js"></script> 11 <script> 12 var unityInstance = UnityLoader.instantiate("unityContainer", "Build/webgl.json", {onProgress: UnityProgress}); 13 var txt1; 14 window.onload = function () { 15 txt1 = parent.document.getElementById("IDValue");18 } 19 function SayHello(arge) 20 { 21 alert(arge); 22 unityInstance.SendMessage("Main Camera", "Receive", txt1.value); 23 } 24 </script> 25 </head> 26 <body> 27 <div class="webgl-content"> 28 <div id="unityContainer" style=" 960px; height: 600px"></div> 29 <div class="footer"> 31 <div class="webgl-logo"></div> 32 <div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div> 33 <div class="title">JsToUnityToDBDemo</div> 34 </div> 35 </div> 36 </body> 37 </html>
在本地调试时parent.document.getElementById("IDValue")报错,放到服务器上不报错:
Uncaught DOMException: Blocked a frame with origin "" from accessing a cross-origin frame
通过查资料得知两个页面需要在同一域名下,所以服务器上没有问题。
补充:
iframe嵌套 子页面 父页面相互取元素和方法(如果有好的可跨域的父与子页面传值的方法,欢迎评论)。