Unable to show alert via Javascript in WP7 web browser control

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:11:48

问题


I am trying to show alert box using javascript in a webpage using web browser control in WP7. The alert is not popping up. Is there anything wrong in the code or WP7 doesn't support it at all?

<phone:WebBrowser Name="browser" IsScriptEnabled="True" ScriptNotify="browser_ScriptNotify" Source="Default.html"/>

Inside Default.html

<html><br>
<head><br>
</head><br>
<body onload="onLoad()"><br>
    <script type="text/javascript"><br>
        function onLoad() {<br>
            alert("hello");<br>
        }<br>
    </script><br>
</body><br>
</html>

回答1:


Are you able to get the default.html resource loaded? Have a look at http://blogs.msdn.com/b/dohollan/archive/2010/08/25/adventures-with-the-windows-phone-7-webbrowser-control.aspx first.

UPDATED TO INCLUDE SAMPLE CODE FOR HOW TO ACHIEVE AN INTENDED EFFECT:

The HTML:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script type="text/javascript" >
        function ShowNameAlert(name) {
            window.external.notify("Hello " + name);
        }
     </script>
 </head>
<body onload="ShowNameAlert('Jojo');">
Bla bla
</body>
</html>

The C# code-behind:

private void SomeBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    MessageBox.Show(e.Value);
}

The XAML:

<phone:WebBrowser x:Name="SomeBrowser" ScriptNotify="SomeBrowser_ScriptNotify" IsScriptEnabled="True" Source="test.html"/>



回答2:


This is how I solved it, I injected the javascript onto the webPage to which I navigate, and overrided the alert and confirm boxes

window.alert = function(__msg){window.external.notify(' + __msg + ');};

Then in the script notify function displayed that message using MessageBox. I hope it helps others too. The previous answer was a workaround, this is what I feel is the correct solution to my problem



来源:https://stackoverflow.com/questions/11843617/unable-to-show-alert-via-javascript-in-wp7-web-browser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!