locked
Dose webview support javascript to operate cookie? RRS feed

  • Question

  • I write such a page to test it:

    <html>
    <head>
    <script type="text/javascript">
    function display_alert(){  alert("I am an alert box!!")   }
    function set_cookie(){ document.cookie="name=abc"}
    function show_cookie(){ var txt = document.getElementById('show'); txt.value=document.cookie}
    </script>
    </head>
    <body><input type="text" id="show" value="unknown" />
    <br/><input type="button" onclick="set_cookie()" value="set cookie" />
    <input type="button" onclick="show_cookie()" value="get cookie" />
    </body>
    </html>

    But it show nothing when I click "get cookie" button. And this page can work in IE and firefox.

    There is a folder named "INetCookies" in local folder, which contain the cookie of WebView, So, does WebView support "document.cookie" to operate cookie ?


    Ray_ni

    Tuesday, September 4, 2012 3:37 AM

Answers

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by NMG1852951 Thursday, September 6, 2012 7:49 AM
    Thursday, September 6, 2012 7:29 AM

All replies

  • Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by NMG1852951 Thursday, September 6, 2012 7:49 AM
    Thursday, September 6, 2012 7:29 AM
  • Can you explain what exactly are you trying to do?

    I have created a simple C#/XAML application, and put in the following code:

       <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <WebView HorizontalAlignment="Left" Height="368" Margin="50,103,0,0" VerticalAlignment="Top" Width="843" Source="http://localhost/test.html"/>
            <TextBlock FontSize="24" HorizontalAlignment="Left" Margin="50,48,0,0" TextWrapping="Wrap" Text="WebView Test" VerticalAlignment="Top" Height="50" Width="458"/>
        </Grid>

    where test.html has the same contents as you have posted.

    When I run the application I am able to click the button and see the value "name=abc" in the text box.

    Saturday, September 22, 2012 1:02 AM
    Moderator
  • Hello, Phadke, Thanks for your reply !

    Will, I have a try. I find it will be OK to operate cookie, to load follow page with WebView.Navigate(Uri uri) to load page. But it can not work, when I use WebView.NavigateToString(string html).

    I want to write cookies to help user to do auto login.


    Ray_ni


    • Edited by NMG1852951 Thursday, November 22, 2012 2:50 AM
    Thursday, November 22, 2012 2:49 AM
  • Hi Ray_ni,

    The problem is happening because when you NavigateToString with the html, the "source" of the WebView is internally not being set to a valid URI scheme. Thus, the cookie does not get set in the first place, so there is no cookie when you try to get it.

    Depending on your scenario, what you could possibly do is to use the Navigate() method to navigate to the page using the URI scheme ms-appx-web:///.... and once the page gets loaded, you can actually call your JavaScript function that invokes the setting of the cookie using this syntax:

    String[] strArgs = new String[1];
    strArgs[0] = "name=efg";
    wvTest.InvokeScript("set_cookie", strArgs);

    where wvTest is the ID of your WebView and you will need to modify your set_cookie JavaScript function to accept a parameter.

    This should cause the cookie to be set as per your need. I guess the only reason you wanted to perform the NavigateToString is because you wanted to have control of the name/value of the cookie, which you can achieve using the above suggested method (of using InvokeScript).

    Thanks,

    Prashant.

    Saturday, December 1, 2012 1:36 AM
    Moderator