The WebBrowser
control allows in-place navigation. Users may click on a link to change the page displayed in the control, and in addition, client-side JavaScript may assign the window.location.href
property to navigate to a new location, which requires that the WebBrowser.IsScriptEnabled
property be set to true.
Allowing the user free rein over navigation may not always be desirable, and you may want to monitor or even cancel the navigation of a web page programmatically. Fortunately, the Navigating
event of the WebBrowser
can be canceled. This is done by subscription to the event in code, or in XAML as shown:
<phone:WebBrowser Navigating="webBrowser_Navigating"/>
When the WebBrowser
begins navigating to a new web page, the event is raised, at which time you can examine the URL and determine whether to allow the navigation:
void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.ToString().IndexOf("www.example.com",
StringComparison.CurrentCultureIgnoreCase) != -1)
{
e.Cancel = true;
}
}
18.116.67.177