问题
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