locked
Disallow link To open in system Browser RRS feed

  • Question

  • I am Using WebView in my windows 8.1 store app in which every (Target=_Blank) type link force a link to open in system browser I want to open it in a webview it self to restrict the link go outside the application is there any way to complete the action?

    Thanks.

    Wednesday, January 29, 2014 4:43 AM

Answers

  • Hi shivani lad,

    Try to inject following JavaScript code to your html page by remove the Target=_Blank.

    string InsertionScriptString = "function disableLink() { ";
    InsertionScriptString += "var links = document.links, i, length;";
    InsertionScriptString += "for (i = 0, length = links.length; i < length; i++) {";
    InsertionScriptString += "links[i].target == '_blank' && links[i].removeAttribute('target');}}";

    By using following statement to execute the script

    webview.InvokeScriptAsync("disableLink", null);

    I have a sample with 8.0 app, but should as same as 8.1 app, take a look:http://sdrv.ms/14e8G5y Hope it helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, January 29, 2014 7:41 AM
    Moderator

All replies

  • Hi shivani lad,

    Try to inject following JavaScript code to your html page by remove the Target=_Blank.

    string InsertionScriptString = "function disableLink() { ";
    InsertionScriptString += "var links = document.links, i, length;";
    InsertionScriptString += "for (i = 0, length = links.length; i < length; i++) {";
    InsertionScriptString += "links[i].target == '_blank' && links[i].removeAttribute('target');}}";

    By using following statement to execute the script

    webview.InvokeScriptAsync("disableLink", null);

    I have a sample with 8.0 app, but should as same as 8.1 app, take a look:http://sdrv.ms/14e8G5y Hope it helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, January 29, 2014 7:41 AM
    Moderator
  • thanks for reply, but i am using xaml with c#. 
    Thursday, January 30, 2014 11:02 AM
  • Hi,

    You can make the links as Buttons or TextBlock controls and handle the tap event by your own.

    It's better if you could explain more on how you're showing your links in the UI.


    My blog : http://thuruinhttp.wordpress.com

    Thursday, January 30, 2014 11:49 AM
  • Yes, the code I provided is work for XAML and C#. Try to inject the Script into your WebView content.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, January 31, 2014 6:25 AM
    Moderator