帮助在C#WebBrowser内容中执行Javascript [英] Help executing Javascript in C# WebBrowser Content

查看:103
本文介绍了帮助在C#WebBrowser内容中执行Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..请帮助我,我真的不知道该怎么做。



我想在C#WebBrowser内容中运行Javascript代码。

我试过在互联网上搜索大多数人说我应该使用InvokeScript()。但它仍然无法正常工作。 :(



这是调用Javascript的代码部分。

Hello guys..please help me here i really don't know what to do.

I would like to run a Javascript code in C# WebBrowser Content.
I've tried searching the internet most of them says that I should use InvokeScript(). However it still didn't work. :(

Here's the part of the code that calls the Javascript.

contentBrowser.Navigate(b); //b contains the url.
contentBrowser.Document.InvokeScript("resize"); //resize is the javascript inside the html.





这是html中的Javascript代码。



Here's the Javascript code inside the html.

function resize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  doc = document.getElementById("Layer1"); //Layer1 a layer for html background.
  doc.style.width = (myWidth-25)+"px"; 
  doc = document.getElementById("Layer4"); //Layer4 a layer for html text.
  doc.style.width = (myWidth-125)+"px";
  doc = document.getElementByTagName("table"); //table for html text.
  doc.style.width = (myWidth)+"px";
}





你能给我任何建议或任何解决我问题的技巧。 :confused:提前谢谢!!



新年快乐!! :cool:



Can you give me any suggestion or any tips to solve my problem. :confused: Thank you in advance !!

HAPPY NEW YEAR!! :cool:

推荐答案

我认为您需要等待网页完全加载才能调用脚本。为此,WebBrowser控件上有一个已完成的事件。如果您只是想快速测试这个理论,请将您的调用脚本代码放在表单上的按钮中。这样,您可以等待页面加载,然后按按钮调用脚本。此外,在测试这个时,我建议你让你的调整大小JavaScript函数做一些非常简单的事情,这样你就可以确定它会在它工作时注意到。例如,您可以将其简化为此(暂时,出于测试目的):

I think you need to wait for the webpage to be fully loaded before you can invoke the script. There is a completed event on the WebBrowser control for this purpose. If you just want to test this theory out real quick, put your invoke script code in a button on your form. That way, you can wait for the page to load, then press the button to invoke the script. Also, while testing this, I suggest you make your "resize" JavaScript function do something really simple so you can be sure you'll notice when it's working. For example, you could simplify it to this (temporarily, for testing purposes):
function resize() { alert("Hello"); }


要添加到我之前的答案,这对我来说很好:

To add to my previous answer, this works fine for me:
namespace WindowsFormsApplication12
{

    using System;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        public Form1()
        {

            // Boilerplate stuff.
            InitializeComponent();


            // Variables.
            DateTime startTime = DateTime.Now;


            // Create web browser.
            WebBrowser browser;
            browser = new WebBrowser();
            browser.DocumentText = @"<html><head><title></title>
                <script type=""text/javascript"">function DoIt(){alert('hello');}</script>
                </head><body>Hello</body></html>";
            browser.Dock = DockStyle.Fill;
            this.Controls.Add(browser);


            // Create button.
            Button btnTest = new Button();
            btnTest.Dock = DockStyle.Bottom;
            btnTest.Text = "Call JavaScript";
            btnTest.Click += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (DateTime.Now.Subtract(startTime).TotalSeconds < 5)
                    {
                        MessageBox.Show("Would you wait a few seconds please!?");
                    }
                    else
                    {
                        browser.Document.InvokeScript("DoIt");
                    }
                });
            this.Controls.Add(btnTest);

        }
    }

}


这篇关于帮助在C#WebBrowser内容中执行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆