Pages

Monday, September 5, 2011

[HOW TO] Intercept javascript events with Windows Phone WebBrower control

If you need intercept javascript events in Windows Phone web browser control, you must use the ScriptNotify event. This event is launched when the window.external.Notify() function is called from javascript code.

First you must enable IsScriptEnabled property.

IsScriptEnabled

Second, you must handle the ScriptNotify event, you could make this from xaml code or from code behind, as you prefer.

webBrowser1.ScriptNotify+=new EventHandler<NotifyEventArgs>(webBrowser1_ScriptNotify);

EventScript

HTML Example:

<html>
<head> 
    <title>Demo Windows Phone</title>
    <script type="text/javascript">
function SendNotify() { window.external.Notify(‘demo’); }
</script>
</head>
<body>
    <div>
        <input type="button" value="Notify" onClick="SendNotify()"/>
    </div>
</body>
</html>

You can load this html code with NavigateToString function from WebBrowser control.

private void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    if (e.Value == "demo")
    {
        MessageBox.Show(e.Value);
    }
}

Note: You can call javascritp functions from c# code, using InvokeScript method.

No comments:

Post a Comment