1 Replies - 1803 Views - Last Post: 23 September 2012 - 04:22 PM Rate Topic: -----

#1 Son_Of_Diablo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-September 11

question Webbrowser navigate to javascript

Posted 19 September 2012 - 05:22 AM

Hello I try to make a webbrowser that navigates to a javascript.

I have this now:

 private void Form1_Load(object sender, EventArgs e)
        {
         int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("test.txt");
            while ((line = file.ReadLine()) != null)
            {
                webBrowser1.Navigate(line);
                this.Text = line;
                System.Threading.Thread.Sleep(5000);
                while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                {
                    MessageBox.Show(line);
                }
                System.Threading.Thread.Sleep(5000);
                webBrowser1.Navigate(textBox1.Text);
                //System.Threading.Thread.Sleep(5000);
                counter++;
            }

            file.Close();
        }

(textBox1.Text is my javascript)
this don't work it askes me if I wanna run the javascript or not and then it does nothing.

but if I make a button like this:

private void button1_Click(object sender, EventArgs e)
        {
                webBrowser1.Navigate(textBox1.Text);
        }


it will load the javascript


So my question is:

How can I make it run the javascript in the Form1_Load?


I have also tried to add the code to a button to see if that works but that just gives me the same error :/

Is This A Good Question/Topic? 0
  • +

Replies To: question Webbrowser navigate to javascript

#2 int0x90  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 14-September 12

Re: question Webbrowser navigate to javascript

Posted 23 September 2012 - 04:22 PM

Instead of making your WebBrowser control navigate to the Javascript, wrap your Javascript in the TextBox with the <script> tags, e.g.:

<script>alert('foo');</script>



And then use the following code to load the Javascript into your WebBrowser control:

private void Form1_Load(object sender, EventArgs e)
{
    this.webBrowser1.Navigate("about:blank");
    if (this.webBrowser1.Document != null)
    {
        this.webBrowser1.document.Write(textBox1.Text);
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1