Adam Hulbert

Hello, I have a problem where my code is not invoking a script (js) in a web browser when the window is loading, but it will invoke if it is on a button press or any user event.

The following is a Form in a C# Windows Application (not ASP.NET)

Code Snippet

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace VtaCombo

{

public partial class frmLogEditor : Form

{

public frmLogEditor()

{

InitializeComponent();

}

//called after constructor

private void frmLogEditor_Load(object sender, EventArgs e)

{

webBrowser1.Navigate(@"C:\SomePath\editor.html");//this document contains the script i want to invoke

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

webBrowser1.Document.InvokeScript("setHTML", new object[] { "Some Text" }); //this never works

}

private void button1_Click(object sender, EventArgs e)

{

webBrowser1.Document.InvokeScript("setHTML", new object[] { "Some Text" }); //this always works

}

}

}

So it seems to be a problem of timing, because the button click always works, but for some reason the DocumentComplete event is still too early to call the script. The html file is quite simple, it is not including any other files, all the html and js is inline in the one file.

Thanks in advance for all replies.



Re: Windows Forms General HTMLDocument.InvokeScript

nobugz

Perhaps your document contains frames. There's a DocumentCompleted event for each frame. Check this thread.





Re: Windows Forms General HTMLDocument.InvokeScript

Adam Hulbert

No thats not it, although the Document Complete event on the web browser does get called twice. The first time the URL is about:blank and the second time it is the url that I want. But even if i make the InvokeScript call on the second time i still get no result, only on the user button press does it work.

Here is a rar of the html i am loading in www.adamhulbert.com/editor.rar it is a simple wysiwyg html/js editor which i have retreived from this tutorial (all that is changed is the added setHTML and getHTML js functions)





Re: Windows Forms General HTMLDocument.InvokeScript

Yu Guo – MSFT

Hi, Adam Hulber,

I tried your codes, and this issue does exist.

And as far as I can see, it is caused by that when the html page is loaded, the JavaScript part is not initialized.(JS engine)

However, I tried to solve it by using the following workaround, adding a timer object.

Code Snippet

Timer timer;

private void frmLogEditor_Load(object sender, EventArgs e)

{

webBrowser1.Navigate(@"E:\editor\editor.html");//this document contains the script i want to invoke

timer = new Timer();

timer.Interval = 1;

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

}

void timer_Tick(object sender, EventArgs e)

{

webBrowser1.Document.InvokeScript("setHTML", new object[] { "Some Text" }); //this always works

timer.Stop();

}

Hopes this helps,

Regards






Re: Windows Forms General HTMLDocument.InvokeScript

Adam Hulbert

Thanks alot Yu, I will try it as soon as I get home from work. Your solution is exactly the kind of thing i was looking for.

Cheers!





Re: Windows Forms General HTMLDocument.InvokeScript

Adam Hulbert

Is it possible that this might not work on a slower machine It fixed the problem when i run my project, but we want to distribute our application to many users with many different computers.

I dont really understand what is happening exactly, does the timer wait one whole second before calling the timer_Tick as far as i can tell, there is no retry if the JS engine hasnt been loaded yet.

Surely there is some kind of event that is triggered when the js engine is loaded i would hate this to become a technical support nightmare for us later. Thanks alot!





Re: Windows Forms General HTMLDocument.InvokeScript

Yu Guo – MSFT

Hi, Adam Hulbert,

Then let's put the codes in DocumentCompleted Event

Code Snippet

Timer timer;

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

timer = new Timer();

timer.Interval = 1;

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

}

void timer_Tick(object sender, EventArgs e)

{

webBrowser1.Document.InvokeScript("setHTML", new object[] { "Some Text" }); //this always works

timer.Stop();

timer.Dispose();

}

I tried it in my virtual machine, and it works well,

but please test more before you release your product.

Hopes this helps,

Regards






Re: Windows Forms General HTMLDocument.InvokeScript

Adam Hulbert

Thanks yo, i've done that. I was thining more along the lines of testing if the InvokeScript passed or failed (if its possible to do this), then if it failed, we can try again in another second.

Thanks again, you've been very helpful.