Handling new window open with WebView RRS feed

  • Question

  • Hi,

    I'm developing an app where I use webview to display a webpage. The webpage has a link where a file gets downloaded automatically when selected. What I'm trying to do is to get the link of the downloadable file and try to download it from my app. The problem I'm facing is that IE automatically opens whenever the downloadable link is pressed. I cant get event handler to detect this scenario. I've seen there could be a solution using Javascript but I dont know how to implement it. I found there is x-ms-webview.MSWebViewNewWindowRequested but this one is for Windows 10 only. I wonder if there is an alternative solution for WP8.1. Thanks

    Wednesday, April 8, 2015 1:26 AM

All replies

  • The easiest way is probably to handle WebView.NavigationStarted and check if the Url is a file type, then cancel the navigation and use HttpClient to download the file.

    Wednesday, April 8, 2015 1:32 AM
  • Hi Rob,

    I've already checked that. This event is not triggered in this case. A new IE window opens automatically and it starts downloading the file. 

    Wednesday, April 8, 2015 1:35 AM
  • It sounds like your page has some specific HTML/JavaScript that you'll need to override then. Details will depend on what exactly that code does, but you'll probably need to inject code to find and modify the code on the page to notify the host page: either rewrite it as an anchor so you can handle NavigationStarted or call window.notify explicitly.
    Wednesday, April 8, 2015 1:47 AM
  • Thanks Rob. I'll try this out..
    Wednesday, April 8, 2015 6:30 AM
  • Hi Rob,

    Now I've tried two solutions and none of them worked as expected. Here are the options

    1) In the WebView.NavigationStarted, I download the html content and then modify it to remove target=_blank which triggers opening a new window. I then call  WebView.NavigateToString to call the new html content. Looks like it works somehow but the reloaded page does not contain images, links, etc. Is there a way to fix this?

    2) I've tried the solution described in this link:


    which is essentially injecting javascript code to handle button clicks.  The code looks like this:

    private void WebView_OnLoadCompleted(object sender, NavigationEventArgs e)
            webView.InvokeScript("eval", new[]
                "var loginButon=document.getElementById(\"login\");" +
                "if (loginButon.addEventListener) {" +
                    "loginButon.addEventListener(\"click\", clickFunction, false);" +
                "} else {" +
                    "loginButon.attachEvent('onclick', clickFunction);" +
                "}  " +
                "function clickFunction(){" +
                    " window.external.notify('loginClick');"+

    However, I get error when the code executes webView.InvokeScript (Execption:0x80020101). Do you have any idea why?

    Kind Regards

    Monday, April 13, 2015 2:37 AM