Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2007, 08:02 PM
Authorized User
 
Join Date: Feb 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using web browser control in C#2.0

Hello,

I am using web browser control in C#2.0. I need to insert the Javascript dynamically in the head section of the page and invoke that script dynamically. Can please anyone tell me how to do that?

p.s. Script doesn't return any values as it is a very simple script.

Thanks.

__________________
A.Asif
 
Old November 1st, 2007, 03:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

 Should be possible by following theses steps:
  • Get a reference to the document
  • get a reference to the head element - getElementsByTagName("head")[0]
  • create a new script element - document.createElement("script")
  • Add the actual script to the script element using innerText
  • Append the script element to the head
If all that is incomprehensible then you need to brush up on the Document Object Model. The process would be the same in a browser so maybe practice on that first to make sure it works.

--

Joe (Microsoft MVP - XML)
 
Old November 1st, 2007, 10:08 AM
Authorized User
 
Join Date: Feb 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
  Should be possible by following theses steps:
  • Get a reference to the document
  • get a reference to the head element - getElementsByTagName("head")[0]
  • create a new script element - document.createElement("script")
  • Add the actual script to the script element using innerText
  • Append the script element to the head
If all that is incomprehensible then you need to brush up on the Document Object Model. The process would be the same in a browser so maybe practice on that first to make sure it works.

--

Joe (Microsoft MVP - XML)
 
Old November 1st, 2007, 10:13 AM
Authorized User
 
Join Date: Feb 2007
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,

Thanks for your suggestion. I'm afraid the SCRIPT element doesn't have an innerText property. If I try to set it in the code I get the following exception "Property is not supported on this type of HtmlElement.".
Here is the code I'm using:
HtmlElement s = LPBrowser.Document.CreateElement("script");
try
 {
    //this line throws an exception
    s.InnerText = "function test(){}";
}
catch(Exception ee)
{}


Do you have any ther suggetion?

Quote:
quote:Originally posted by joefawcett
  Should be possible by following theses steps:
  • Get a reference to the document
  • get a reference to the head element - getElementsByTagName("head")[0]
  • create a new script element - document.createElement("script")
  • Add the actual script to the script element using innerText
  • Append the script element to the head
If all that is incomprehensible then you need to brush up on the Document Object Model. The process would be the same in a browser so maybe practice on that first to make sure it works.

--

Joe (Microsoft MVP - XML)
 
Old November 1st, 2007, 10:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The following seems to work:

webBrowser1.Document.Write("<script>document.write ('hello world')</script>");

/- Sam Judson : Wrox Technical Editor -/
 
Old November 1st, 2007, 12:01 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, you can perhaps set the innerHTML or append the actual script as a text node. Use createTextNode and appendChild.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Browser Control AccessAasif@aol.com C# 2005 1 January 9th, 2008 06:26 AM
Browser control in C# 2.0 AccessAasif@aol.com C# 2005 1 November 10th, 2007 06:56 AM
Problem with Web Browser control in vc++ sureshkumarct Visual C++ 0 May 29th, 2006 04:01 AM
Web Browser Activex Control gdelucchi Access 0 February 20th, 2006 02:18 PM
web control not displaying in browser rj1406 ASP.NET 1.0 and 1.1 Basics 8 September 19th, 2004 05:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.