locked
how to use foreach loop with webview.invokescrypt() in c# store app ? RRS feed

  • Question

  • HtmlElementCollection elmnts = webBrowser1.Document.GetElementsByTagName("div");
                foreach (HtmlElement elemnt in elmnts)
                {
                    if (elemnt.GetAttribute("classname") == "description")
                    {
                        textBox1.Text = elemnt.OuterText;
                    }
                }

    Is there anyway to use this code with webview.InvokeScrypt() in visual c# store app development ?

    "HtmlElementCollection elmnts = webBrowser1.Document.GetElementsByTagName("div");" this line is okay..

    All I wanna know is how to use if condition with invokescrypt ?

    • Moved by Caillen Thursday, January 2, 2014 6:01 AM Windows Store apps related question.
    Wednesday, January 1, 2014 3:00 PM

Answers

  • Hi AstroHaloce,

    Use following code should be fine, also this is the sixth section of the blog I suggested.

    Remove the source of the WebView:

    <WebView x:Name="MyWebview" Width="500" Height="500" ScriptNotify="MyWebview_ScriptNotify_1"/> 
    

    Inject the JavaScript to a HTML page:

     Windows.Storage.StorageFile MyWebPageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///HTMLPage1.html"));
     string MyWebPageString = await FileIO.ReadTextAsync(MyWebPageFile);
     string ScriptTagString = "<script>";
     int IndexOfScriptTag = MyWebPageString.IndexOf(ScriptTagString);
     int LengthOfScriptTag = ScriptTagString.Length;
     string InsertionScriptString = "function SayHelloWorld() { window.external.notify(\"Hello World!\");} ";
     MyWebPageString = MyWebPageString.Insert(IndexOfScriptTag + LengthOfScriptTag + 1, InsertionScriptString);
     MyWebview.NavigateToString(MyWebPageString);

    InvokeScript in WebView:

    private void MyButton_Click_1(object sender, RoutedEventArgs e)
     {
     MyWebview.InvokeScript("SayHelloWorld", null);
     }

    --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 3, 2014 1:41 AM
    Moderator

All replies

  • Can you give some more detail about what you are trying to achieve?  An if statement needs a condition to test and then executes code depending on the value of the condition.  What does that have to do with InvokeScript (I searched but couldn't find InvokeScrypt, I assume you mean InvokeScript)

    if (do some test here) {

       statements to execute if the test passes

    } else {

       statements to execute if the test fails

    }


    Paul Linton

    Thursday, January 2, 2014 1:03 AM
  • My code checks the all the <div> tags in the web page and if the classname of the selected <div> it shows the text in that <div>. This code is only works with c# windows forms... I want to do that task for a winrt 

    Thursday, January 2, 2014 6:57 AM
  • Hi AstroHaloce,

    I would suggest you to read this blog first before you start working with WebView: http://blogs.msdn.com/b/wsdevsol/archive/2012/10/18/nine-things-you-need-to-know-about-webview.aspx, if you need invokeScript, you should read the fourth part: How to invoke Javascript inside the displayed webpage of a Webview. By the way, if you are working with 8.1 app, use InvokeScriptAsync method instead.

    HtmlElementCollection is a WinForm API which is not acceptable with WinRT.

    --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.


    Thursday, January 2, 2014 7:12 AM
    Moderator
  • I checked it. but I couldnt find the answer !
    Thursday, January 2, 2014 7:32 AM
  • We can only inject JavaScript codes into WebView and there is no HTMLDocument/HTMLElement exist in WinRT.

    The scenario should be like: write some JavaScript methods and inject them to HTML in WebView, then use InvokeScript() to call these methods. This is the only way to work with WinRT webview.

    --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.


    Thursday, January 2, 2014 7:46 AM
    Moderator
  • which class should I use to inject javascripts to the webview ?
    Thursday, January 2, 2014 3:35 PM
  • Hi AstroHaloce,

    Use following code should be fine, also this is the sixth section of the blog I suggested.

    Remove the source of the WebView:

    <WebView x:Name="MyWebview" Width="500" Height="500" ScriptNotify="MyWebview_ScriptNotify_1"/> 
    

    Inject the JavaScript to a HTML page:

     Windows.Storage.StorageFile MyWebPageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///HTMLPage1.html"));
     string MyWebPageString = await FileIO.ReadTextAsync(MyWebPageFile);
     string ScriptTagString = "<script>";
     int IndexOfScriptTag = MyWebPageString.IndexOf(ScriptTagString);
     int LengthOfScriptTag = ScriptTagString.Length;
     string InsertionScriptString = "function SayHelloWorld() { window.external.notify(\"Hello World!\");} ";
     MyWebPageString = MyWebPageString.Insert(IndexOfScriptTag + LengthOfScriptTag + 1, InsertionScriptString);
     MyWebview.NavigateToString(MyWebPageString);

    InvokeScript in WebView:

    private void MyButton_Click_1(object sender, RoutedEventArgs e)
     {
     MyWebview.InvokeScript("SayHelloWorld", null);
     }

    --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 3, 2014 1:41 AM
    Moderator