locked
c++/xaml webview loading links in other applications

    Question

  • I am trying to load a page/site using webview. The page loads fine but when I click on any link it opens with default internet browser installed on my computer, but I want it to load it there itself. I also downloaded webview sample. In the sample it loads any links in the page within itself, so the mistake must be mine. I am trying to figure it out for hours, but nothing came up. Here is my code

    String^ url = "http://www.microsoft.com";

    Uri^ targetUri = ref new Uri(url);

    webview001->Navigate(targetUri);



    • Edited by GetsugaTenshouF Wednesday, October 29, 2014 5:53 PM
    • Moved by Shu 2017 Thursday, October 30, 2014 10:05 AM your issue is more related to store app
    Wednesday, October 29, 2014 5:52 PM

Answers

  • Hi GetsugaTenshouF,

    Thanks for posting in MSDN forum.

    I guess your problem is caused by the tag "target="_blank", so it is alway navigate to the default  browser. I have made a sample to test it. My test html is a simple page with a link to google.

    String^ webpagestring = L"<!DOCTYPE html>\n" +
    		"<html>\n"
    		"<head>\n" +
    		"<meta charset = \"utf-8\"/>\n" +
    		"<title>James Testing</title>\n" +
    		"<script lang=\"en-us\" type=\"text/javascript\">\n" +
    		"</script>\n" +
    		"</head>\n" +
    		"<body>\n" +
    		"<a target = \"_blank\" href = \"https://www.google.com.hk/?gws_rd=ssl\">Click to Navigate</a>\n" +
    		"</body>\n" +
    		"</html>";

    I write a Javascript function to remove the tag "target="_blank". Here are some code snippet:

    std::wstring ScriptTagString = L"<script lang=\"en-us\" type=\"text/javascript\">"; std::wstring ws1(webpagestring->Data()); int IndexOfScriptTag = ws1.find(ScriptTagString); int LengthOfScriptTag = ScriptTagString.length();

    //Positioning String^ InsertionScriptString = L"function disableLink() { "; InsertionScriptString += L"var links = document.links, i, length;"; InsertionScriptString += L"for (i = 0, length = links.length; i < length; i++) {"; InsertionScriptString += L"links[i].target == '_blank' && links[i].removeAttribute('target');}}\n"; std::wstring ws2(InsertionScriptString->Data()); ws1 = ws1.insert(IndexOfScriptTag + LengthOfScriptTag + 1,ws2 );

    //add the function to my test page String^ webhtm = ref new String(ws1.c_str()); webview->NavigateToString(webhtm);


    Then invoke the JS function by C++:

            String^ scr = ref new String(L"disableLink");
    	webview->InvokeScript(scr, nullptr);

    Finally, my sample project is able to load a page on my own webview control. Hope it helps.

    Best regards,

    Shu Hu

    Thursday, October 30, 2014 9:41 AM
  • How do you link the css files to your html? Inline css file should be ok, so have you check the ref path is correct? It would be nice for us to help you if you can share your code or project with use. 

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 31, 2014 1:28 PM

All replies

  • Hi GetsugaTenshouF,

    Thanks for posting in MSDN forum.

    I guess your problem is caused by the tag "target="_blank", so it is alway navigate to the default  browser. I have made a sample to test it. My test html is a simple page with a link to google.

    String^ webpagestring = L"<!DOCTYPE html>\n" +
    		"<html>\n"
    		"<head>\n" +
    		"<meta charset = \"utf-8\"/>\n" +
    		"<title>James Testing</title>\n" +
    		"<script lang=\"en-us\" type=\"text/javascript\">\n" +
    		"</script>\n" +
    		"</head>\n" +
    		"<body>\n" +
    		"<a target = \"_blank\" href = \"https://www.google.com.hk/?gws_rd=ssl\">Click to Navigate</a>\n" +
    		"</body>\n" +
    		"</html>";

    I write a Javascript function to remove the tag "target="_blank". Here are some code snippet:

    std::wstring ScriptTagString = L"<script lang=\"en-us\" type=\"text/javascript\">"; std::wstring ws1(webpagestring->Data()); int IndexOfScriptTag = ws1.find(ScriptTagString); int LengthOfScriptTag = ScriptTagString.length();

    //Positioning String^ InsertionScriptString = L"function disableLink() { "; InsertionScriptString += L"var links = document.links, i, length;"; InsertionScriptString += L"for (i = 0, length = links.length; i < length; i++) {"; InsertionScriptString += L"links[i].target == '_blank' && links[i].removeAttribute('target');}}\n"; std::wstring ws2(InsertionScriptString->Data()); ws1 = ws1.insert(IndexOfScriptTag + LengthOfScriptTag + 1,ws2 );

    //add the function to my test page String^ webhtm = ref new String(ws1.c_str()); webview->NavigateToString(webhtm);


    Then invoke the JS function by C++:

            String^ scr = ref new String(L"disableLink");
    	webview->InvokeScript(scr, nullptr);

    Finally, my sample project is able to load a page on my own webview control. Hope it helps.

    Best regards,

    Shu Hu

    Thursday, October 30, 2014 9:41 AM
  • that helped, but for me I guess just this line is causing all the trouble

    <base target='main'>

    Its in the <head> of the website that I am trying to load. How can I remove it?(maybe first download it then remove it and then use webview to navigate it?)

    Thursday, October 30, 2014 4:15 PM
  • Thanks that I helped a lot, another problem is that the code wont load any css files
    Thursday, October 30, 2014 6:03 PM
  • How do you link the css files to your html? Inline css file should be ok, so have you check the ref path is correct? It would be nice for us to help you if you can share your code or project with use. 

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 31, 2014 1:28 PM