Adding a script in the head tag

Add-in Express™ Support Service
That's what is more important than anything else

Adding a script in the head tag
i'm trying to add a script into the head tag but i'm always having a null head tag object 
Fady Sayegh

Posts: 7
Joined: 2012-09-12
Hi All,

i'm trying to ad a script to the head tag of a document. however i'm always having a null value after getting the head tag for the element collection.
please check the below snipped code and point me in the right direction.

mshtml.HTMLDocument oHtmlDoc = this.HTMLDocument;

mshtml.HTMLHeadElementClass oHtmlHead = ((mshtml.IHTMLElementCollection)oHtmlDoc.all.tags("head")).item(null, 0) as mshtml.HTMLHeadElementClass;

if (oHtmlHead != null)
mshtml.IHTMLScriptElement script = ((mshtml.IHTMLScriptElement)oHtmlDoc.createElement("script"));

script.type = "text/javascript";
script.text = "SOME SCRIPT";

can you please indicate what is the problem with the above code.

Best regards and thanks in advance

Fady Sayegh
Posted 19 Sep, 2012 03:08:59 Top
Eugene Astafiev


Hi Fady,

I have just tried to run your code on my PC with Internet Explorer 9 installed and couldn't reproduce the issue. Your code works without any exceptions or error messages. Also please take a look at the similar Cannot inject the script forum thread. Did you try to use the code listed there?
Posted 19 Sep, 2012 09:25:14 Top