{"id":100615,"date":"2019-01-02T07:00:00","date_gmt":"2019-01-02T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100615"},"modified":"2019-03-13T00:02:15","modified_gmt":"2019-03-13T07:02:15","slug":"20190102-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190102-00\/?p=100615","title":{"rendered":"How can I prevent a WebView control from opening a browser window?"},"content":{"rendered":"<p>A customer had an application that used a UWP <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ui.xaml.controls.webview\">WebView<\/a> control. Some Web sites open links in a new window by using techniques like <code>TARGET=_blank<\/code>. When the user clicks on such a link, it opens in a Web browser. The customer wanted to know how to prevent this. <\/p>\n<p>To do this, you can handle the <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.ui.xaml.controls.webview.newwindowrequested\">New&shy;Window&shy;Requested<\/a> event. You can mark the event as <code>Handled<\/code>, in which case the system will consider the action complete and will not send the request to the user&#8217;s default Web browser. <\/p>\n<\/p>\n<pre>\n&lt;!-- XAML --&gt;\n&lt;WebView NewWindowRequested=\"OnNewWindowRequested\" \/&gt;\n\n\/\/ C# code-behind\nvoid OnNewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs e) \n{ \n    \/\/ Block all requests to open a new window\n    e.Handled = true; \n} \n<\/pre>\n<p>You can inspect the <code>Referrer<\/code> and <code>Uri<\/code> properties to learn more about what triggered the new window. <\/p>\n<ul>\n<li><code>Referrer<\/code> is the page that wants to open the window.<\/li>\n<li><code>Uri<\/code> is the page that it wants to open.<\/li>\n<\/ul>\n<p>If your handler is a coroutine, then you must set <code>Handled = true<\/code> before performing any <code>await<\/code> operations, because the handler returns to its caller as soon as you perform an <code>await<\/code>, and the rest of the handler runs as an asynchronous task. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It will check with you first, but you have to be listening.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-100615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It will check with you first, but you have to be listening.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=100615"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=100615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}