Click here to Skip to main content
15,885,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


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";
}


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

HAPPY NEW YEAR!! :cool:
Posted

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):
JavaScript
function resize() { alert("Hello"); }
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-Dec-10 20:13pm    
Confirmed! Calling a script function directly after navigating to a page will most likely result in the function not yet being defined. Wait for the document completed event.

Good call! 5+
jovhenni19 29-Dec-10 11:20am    
I think there's something wrong with my laptop..because my girlfriend tried the simple same code of..

<pre>function resize() { alert("Hello"); }</pre>

well it worked for her. :(

Can you help me find the reason why is that, what's wrong with my laptop. or what am i doing wrong?..

Thanks for the reply! :)
AspDotNetDev 29-Dec-10 13:19pm    
Did you modify to account for the possibility that the JavaScript may not load right away? If not, then it could just be that your computers handle threads differently (e.g., if one is multicore). Also, create a simple HTML page and test if that JavaScript is working at all (without using InvokeScript).
jovhenni19 29-Dec-10 14:24pm    
I've tried creating a simple HTML page and tested it. It works. But when I use InvokeScript in C# it doesn't. Hmmm I have a Intel Centrino Duo, does that make a change?..hehe
AspDotNetDev 29-Dec-10 15:54pm    
Like I said, try using InvokeScript in a button rather than right after navigating to the page. If that works, then try the completed event (that can get a bit more complicated).
To add to my previous answer, this works fine for me:
C#
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);

        }
    }

}
 
Share this answer
 
v3
Comments
Meysam Toluie 1-May-13 4:43am    
My Problem is more complicated. The WebBrowser in my windows application doesn't fire any javascript even when I click on the link using webBrowser UI.
AspDotNetDev 1-May-13 22:19pm    
I am not sure if you are asking me a question, but maybe my tip will help you: http://www.codeproject.com/Tips/130267/Call-a-C-Method-From-JavaScript-Hosted-in-a-WebBro Otherwise, ask a question here: http://www.codeproject.com/Questions/ask.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900