Failed to show alert via Javascript in WP7 browser

I am trying to show a warning window using javascript on a webpage using web browser control in WP7. A warning does not appear. Is there something wrong in the code, or does WP7 not 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
source share
2 answers

Here is how I solved it, I injected javascript into the web page I’m navigating to, and redefined the alert and confirmed the fields

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

Then, in the notification function of the script, this message is displayed using the MessageBox. Hope this helps others as well. The previous answer was a workaround, this is what I think is the right solution to my problem.

+1
source

Can you download the default.html resource? First look at http://blogs.msdn.com/b/dohollan/archive/2010/08/25/adventures-with-the-windows-phone-7-webbrowser-control.aspx .

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

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> 

C # code:

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

XAML:

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