本文介绍了如何在metro应用程序中从Web上下文(Iframe)提供javascript警报。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的地铁应用程序..我使用iframe加载一个Web应用程序 - 基本上是一个包含一些控件的表单,最后用户点击完成按钮,我想显示警告 我知道在地铁应用程序。我们可以使用new Windows.UI.PopupMessage来显示警报。但我如何从网络上下文(Iframe)做同样的事情。 我在default.js中有一个函数(showalert();),我使用new Windows.UI.PopupMessage来显示消息。如果我试图从iframe页面访问此功能,如window.parent.showalert();。我得到例外说访问被拒绝。 请有人回复这个,因为这对我来说非常关键。 谢谢&问候 Goutham 解决方案你可以使用HTML 5的显示(或者您可以在本地环境中执行您想要执行的任何操作)。 请注意消息的参数事件回调( msg here)还包括一个origin属性,您可以检查以确保您只处理来自预期发件人的邮件。 IFRAME中托管的网页 postMessage $ c> onclick 按钮的事件处理程序(您可能希望将调用拉为单独的.js文件而不是内联) <!DOCTYPE html> < html> < head> < title>< / title> < / head> < body> 这是webpage.html加载到iFrame < button id =buttononclick =window.parent.postMessage('Hello from Web Context','*');> Say你好< /按钮> < / body> < / html> In my metro app ..I am using iframe to load a web application - basically a form which contains some controls and finally user click on finish button and i want to show alerti know in metro app. we can give the alert using "new Windows.UI.PopupMessage" to show the alert. but how can i do the same from the web context(Iframe).i have a function (showalert();) in my default.js where i am using the "new Windows.UI.PopupMessage" to show messages. if i am trying to access this function from iframe page like "window.parent.showalert();". i get exception saying access denied.Please someone reply to this as this is very critical for me.thanks & regardsGoutham 解决方案 You can use HTML 5's postMessage to communicate between contexts.Below is an image of a simplistic example with the relevant code snippets following it; the Bing Maps trip optimizer example uses this same technique on a grander scale.The main page (default.js), which is running in the local context, includes an IFRAME loaded in web context via the following markup (I left out the unchanged <head> element to save space):<body onload="localContext.onLoad();"> <p style="margin-top: 150px">This is default.html in the local context</p> <div style="background-color: azure; width: 300px"> <iframe src="ms-appx-web:///webpage.html" /> </div></body>localContext is defined in default.js asvar localContext = { onLoad: function () { window.attachEvent("onmessage", function (msg) { if (msg.origin == "ms-appx-web://bfddc371-2040-4560-a61a-ec479ed996b0") new Windows.UI.Popups.MessageDialog(msg.origin).showAsync().then(); }); }};and it defines an onLoad function for default.html that registers a listener to the onmessage event, and when that event fires a MessageDialog is shown (or you can take whatever action you want to do in the local context).Note that the parameter to the message event callback (msg here) also includes a origin property that you can check to make sure you're only handling messages from expected senders.The web page hosted in the IFRAME calls postMessage in the onclick event handler of a button (you'll probably want to pull the invocation a separate .js file versus in-line)<!DOCTYPE html><html> <head> <title></title> </head> <body> This is webpage.html loaded into an iFrame <button id="button" onclick="window.parent.postMessage('Hello from Web Context', '*');">Say Hello</button> </body></html> 这篇关于如何在metro应用程序中从Web上下文(Iframe)提供javascript警报。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 01:41