1

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

2

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"/>
Sign up to request clarification or add additional context in comments.

3 Comments

The page is opening properly but the alert is not coming
Oh good. I have tried to get the alert to show as well but to no success. I would guess that it probably has to do with the visual composition. That is window.prompt is generally rendered on top of the window object but since that is now embedded inside the silverlight app, there are some policy issues that prevent that. (As a side note, ScriptNotify is used for browser to silverlight communication and hence this is not the bridge to get the alert to work. If you want a sample code for how ScriptNotify could be used then i can write here)
Thank you, but i know how to do that. Thanks for your answer, I had already tried that. Just wanted to know if there was any glitch in the code or with WP7 policies. None the less your ans is still right ;)
1

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

5 Comments

I think it is difficult for people answering to questions to really understand whether their solutions are workarounds or correct. At least for me your proposal above is also workaround as you are modifying the content of the pages you are navigating to in order to fit the bill of your application. Shortly, it is not motivating to spend time understanding what people are asking and write-test solutions, have those solutions accepted (as being correct) and then later have them rejected.
I hope you can see that all you are doing in your case is just aliasing the ShowNameAlert in my example to window.alert! I am hoping that you will revert your decision on my solution to an answer :)
Your solution is also right, but in that case I had to modify the html page which I dont want to (thats not my priority). Through this which I found on a bit of research, I was able to add the JS to the html page via my .cs code. For my problem this proves correct. That's why I changed it this answer. Although I appreciate your answer as a addition to this answer. Thanks for your help :)
The solution to the problem is not about injecting Javascript. That is just a delivery mechanism of the workaround which is the solution. Another delivery mechanism is modifying the HTML page. The problem is that alert does not show and i explained why it does not show and hence how to get the effect with the message box. If you asked how do deliver the workaround into the content of the browser control without altering page then i would have told you that. Well, it is disappointing and let's leave it to that ;)
maybe I framed my question wrongly, thank you Gros for your help. I will keep in mind the next tym I post a question to describe all the scenarios. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.