postMessage 方法接受两个参数: message:要发送的消息内容,可以是任何基本数据类型或复杂对象(如字符串、对象、数组等)。 targetOrigin:目标窗口的源(origin),用于限制接收消息的窗口。出于安全考虑,应该明确指定目标源,而不是使用通配符 *。3. 编写代码使用 window.parent.postMessage 进行传参 以下是一个示例,展示如...
//window.parent 是 iframe 子页面获取父页面的 window 对象//后面的 * 号就是处理跨域问题的,任何域名都不会出现跨域问题window.parent.postMessage("需要传递的参数",'*')//也可以指定传送域名地址,这个域名不会出现跨域问题,写父页面(接收)域名地址window.parent.postMessage("需要传递的参数",'http://0.0....
window.postMessage()里面的window对象,是发送消息的目标窗口。比如,父窗口通过window.open()打开子窗口,那么子窗口可以通过targetWindow = window.opener获取父窗口。再比如,父窗口通过iframe嵌入了子窗口,那么子窗口可以通过window.parent获取父窗口。 参数和返回值 ...
window.postMessage()提供了一个受控的机制相对来安全地规避这个限制。 发送消息的基本语法: targetWindow.postMessage(message, targetOrigin,[transfer]); targetWindow就是接收消息的窗口的引用。 获得该引用的方法包括: Window.open Window.opener HTMLIFrameElement.contentWindow Window.parent Window.frames +索引值 m...
window.postMessage的使用 window.postMessage otherWindow.postMessage(message,targetOrigin,[transfer]); otherWindow:其他窗口的一个引用,例如iframe的contentWindow属性 等等窗口对象才能使用; targetOrigin:指定哪些窗口能接收消息事件; window.parent.postMessage(“getOrgInfo”,’*’) 发送事件 window.addEventListener(...
submit(){// 向父vue页面发送信息window.parent.postMessage({data:{code:"success",test:"我是子页面的test!"}},'*');} 1. 2. 3. 4. 5. 6. 7. 8. 9. 父页面代码: methods:{handleMessage(event){constdata=event.data.dataif(data.code==="success"){alert(data.test)}}}mounted(){window...
{window._postMessage("childFunction","my is params",function(data){alert("parent: callChild childFunction(), return "+data);console.log("p:"+data);})}/*** 支持直接对子页面进行调用,而且子页面处理完成会回调 _callback 方法* @param _method iframe中的函数* @param _params 参数* @param ...
window.addEventListener('message', function (e) { console.log(e.data); //处理数据; var data = JSON.parse(e.data); if (data) { data.number = 16; //处理后再发回domain1 window.parent.postMessage(JSON.stringify(data), 'http://test.com/'); ...
window.open()官网的说明是:窗口接口的open()方法将一个指定的资源加载到一个新的或现有的浏览环境中(即一个标签、一个窗口或一个iframe),并使用一个指定的名称。官网 window.open(url,target,windowFeatures)open(iframeUrl,'_blank',params);target:'_blank'//_self, _blank, _parent, and _topwindowFeatu...