user1692121
user1692121

Reputation: 27

Call JavaScript function specified in a child <frame>

Here's the common HTML structure of parent page (index.html):

<html>
<head>
<title>test title</title>
</head>
    <frameset cols="150,*">
        <frame name="left" src="testleft.html">
        </frame>
        <frame name="right" src="testright.html">
        </frame>
  </frameset>
 </html>

Here's the common HTML structure of child page (testleft.html):

<html>
    <head>
        <script>
            function proc1(frm) {
                alert("called proc1");
                frm.setAttribute("id", "change!!");
            }
        </script>
    </head>
    <body>
        <p>This is testpage.</p>
        <form name="TestFormName" action="get" id="TestFormId">
            <input type="text" value="TextValueEmpty" />
        </form>
    </body>
</html>

I want to call proc1() on testleft.html using C#. I tried to use WebBrowser.Document.InvokeScript(), but it can't call proc1(). How do I do that?

Upvotes: 1

Views: 844

Answers (1)

Henrik Karlsson
Henrik Karlsson

Reputation: 5723

WebBrowser.Document.InvokeScript() can only be used to call JScript functions, but since your function is deeper we have to create a small "function" to access your function. The WebBrowser control doesn't have any way of doing that, so we have to use a "hack":

We will navigate to javascript:code in order to execute our code. In order to reach our function proc1, we have to use this small snippet: top.frames[0].proc1().

So, to put it all together, we have to write one simple line of C# code:

WebBrowser.Navigate("Javascript:top.frames[0].proc1()");

Upvotes: 2

Related Questions