Share via


Can't include and run javascript in webbrowser control

Question

Tuesday, January 5, 2010 5:03 PM

Im using webbrowser control in visual 2008, and Internet explorer 8,
I just add following code to display in webbrowser control :

Webbrowser browser = new  Webbrowser();
browser.DocumentText = "<html><head><script src='c:\test.js' type='text/javascript' /></head><body></body></html>";

But every time I run application, the alert windows show this error content :

"An error has occurred in the script on this page
Line: 1
Char: 1
Code: 0
URL: file:///c:/test.js"

And I can't call any javascript function in test.js file, I did try some solutions from another post with the same problem in this forum but it's still not working.

Please help me find out solution for this error !

All replies (7)

Wednesday, January 6, 2010 4:13 AM âś…Answered | 2 votes

Finally I found out solution, To avoice error rasing when you add : "<script src='....' ></script>" in html content of webbrowser control by setting DocumentText property, we can do by differnt way :

  • Create a html file and write all javascript in that html file, ex: "browser.html"
  • Instead of writing html content by DocumentText property, we will navigate our "browser.html" file
  • On document loaded event, we will invoke javascript function to insert html content

Now, it's working well. Thanks for your caring on my problem,Farooqua.


Tuesday, January 5, 2010 5:27 PM

You should either escape the backslash "\ or put an "@" just before the double-quotes. Use this:

browser.DocumentText = "<html><head><script src='c:\test.js' type='text/javascript' /></head><body></body></html>";

Hope that works.

Thanks


Wednesday, January 6, 2010 2:16 AM

Hi, thanks for ur help, but do u see your code is exact the same as my code :

Webbrowser browser = new  Webbrowser();
browser.DocumentText = **"<html><head><script src='c:\test.js' type='text/javascript' /></head><body></body></html>";
*But it still raised error window !
*
**Somebody said that it relate to the security setting in IE, and ask me add "about:blank" to trusted page in IE, but that's not really good solution !

I want to know reasons caused that error and wanna know the solution to avoid that without changing anything in IE or Registry !
Please tell me the way, thanks !


Wednesday, January 6, 2010 3:25 AM | 1 vote

Oh sorry, I forgot to put the "@" sign before the doublequotes.

Try this:

browser.DocumentText = @"<html><head><script src='c:\test.js' type='text/javascript' /></head><body></body></html>";

Thanks


Friday, February 11, 2011 7:42 PM

Finally I found out solution, To avoice error rasing when you add : "<script src='....' ></script>" in html content of webbrowser control by setting DocumentText property, we can do by differnt way :

  • Create a html file and write all javascript in that html file, ex: "browser.html"
  • Instead of writing html content by DocumentText property, we will navigate our "browser.html" file
  • On document loaded event, we will invoke javascript function to insert html content

Now, it's working well. Thanks for your caring on my problem,Farooqua.

Hello, please, share your code :)

mi code is :

            htmhead += "<!--[if IE]><script language=\"javascript\" type=\"text/javascript\" src=\"file:///" + System.IO.Directory.GetCurrentDirectory().ToString() + "\\excanvas.min.js\"></script><![endif]-->";
            htmhead += "<script language=\"javascript\" type=\"text/javascript\" src=\"file:///" +  System.IO.Directory.GetCurrentDirectory().ToString() + "\\jquery.js\"></script>";
            htmhead += "<script language=\"javascript\" type=\"text/javascript\" src=\"file:///" + System.IO.Directory.GetCurrentDirectory().ToString() + "\\jquery.flot.js\"></script>";
            htmhead +=bla bla bla
       wb.Refresh();
            wb.Navigate("about:blank");
            wb.Document..Write(htmhead);
but the error is the same :(

Tuesday, March 29, 2011 4:24 PM

Okay, i dont quite understnd. I made my own browser in vs2010. but when i went to a web site that has html effects, it doesnt run the effects. Why? So i have to add in this codes?

htmhead += "<!--[if IE]><script language=\javascript\ type=\text/javascript\ src=\file:///" + System.IO.Directory.GetCurrentDirectory().ToString() + "\excanvas.min.js\></script><![endif]-->";
            htmhead += "<script language=\javascript\ type=\text/javascript\ src=\file:///" +  System.IO.Directory.GetCurrentDirectory().ToString() + "\jquery.js\></script>";
            htmhead += "<script language=\javascript\ type=\text/javascript\ src=\file:///" + System.IO.Directory.GetCurrentDirectory().ToString() + "\jquery.flot.js\></script>";

            htmhead +=bla bla bla
WHERE SHOULD U PUT THOSE? IN THE DOEUMENT.LOAD OR COMPLETE?

IT DOESNT MAKE SENSE...... my friend told me i have to download Grasshopper for vs..

Which one works?


Tuesday, February 11, 2014 5:30 PM

Thank you Farooqua and Knonk. You saved my project!!!

Tarh ik