我正在使用在此处找到的示例应用程序Projection ,这使我可以打开一个主应用程序窗口,并在第二台显示器上显示其他内容。
现在,我想要的是两个窗口之间的javascript具有某种共享状态或传递消息的方式。在场景1.js中,用于启动新窗口的代码如下所示:
var view = window.open("ms-appx:///html/secondaryView.html", null, "msHideView=yes");
现在,我知道该变量具有view.postMessage()
功能,但是在新窗口的javascript(secondaryView.js)中,我不知道如何监听消息。我也不知道在两个窗口之间是否还有其他显而易见的共享状态的方式,我没有想到过。
[edit]此处提供的localStorage解决方案很好并且可以运行,但是不如使用view.postMessage
在第一个窗口中的任何JavaScript代码中,您都可以发布如下消息:
view.postMessage({ a: 'b' }, '*'); // may replace * with the beginning of the result of window.location.href
在第二个窗口中,此代码将收到以下消息:
window.addEventListener('message',
function(event) {
var message = event.data; // event.data = { a: 'b' }
});
要将消息从第二个窗口发布到第一个窗口,这非常相似,但是用于window.opener
引用打开该视图的视图:
window.opener.postMessage({ a: 'this is from the second window' }, '*');
并再次在原始javascript程序中注意 window.addEventListener('message', (event) => {})
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage
感谢Raymond Chen
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句