Unable to show alert via Javascript in WP7 web browser control

3.9k Views Asked by At

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>
2

There are 2 best solutions below

5
On BEST ANSWER

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

3
On

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"/>