View Full Version : script tag adding internal code with DOM ie problems

05-31-2004, 10:32 AM
Hi everyone, i have a problem with the script tag and with internet explorer.
Just remark here that ALWAYS problems start and finish with internet explorer, as mozilla (firefox 0.8) works always fine.

My code:
var counter_scr = document.createElement('script');
counter_scr.appendChild(document.createTextNode('counter(document.forms.bform_articlesForm.body,8000 );'));

where articles_output is a div.

It does work in mozilla, but not in ie (6.0).
The problem is the counter_scr.appendChild lines.

Any suggestion?? Thanks!

06-01-2004, 06:50 AM
Try replacing



counter_scr.type = 'text/javascript';

06-01-2004, 04:03 PM
var counter_scr = document.createElement('script');
counter_scr.type = 'text/javascript';
counter_scr.appendChild(document.createTextNode('counter(document.forms.bform_articlesForm.body,8000 );'));

ie6.0, hola and hola2 appears, hola3 do not.
any idea?

06-01-2004, 04:50 PM
The problem lies in appendChild. Ie6w gives me an "invalid pointer" message when I try to use it on a script element. However, I tested using the following, which seems to work in moz, op7.50, ie5w, ie5.5w and ie6w at least.
<TITLE>Various browsers testcase: Parsing of dynamically created embedded scripts</TITLE>
<SCRIPT TYPE="text/javascript">
var script=document.createElement('script');
script.text='alert(\'Dynamically created embedded script has been parsed\');'

alert('Embedded script test:\n '+
'SCRIPT element: '+script+'\n '+
'TYPE attribute: '+script.getAttribute('type')+'\n '+
'parent element: '+script.parentNode+'\n '+
'outerHTML property: '+(script.outerHTML||(new XMLSerializer).serializeToString(script))+'\n '+
'innerHTML property: '+script.innerHTML+'\n '+
'text property: '+script.text+'\n '+
('normalised textual contents: '+(script.normalize(),script.firstChild.data)):
<H1>Various browsers testcase: Parsing of dynamically created embedded scripts</H1>
<P>There should technically be no difference between if HTML and XML is created dynamically through the DOM or included in the original page source.
<P>There should be two alerts from this page. One "Dynamically created embedded script has been parsed" and one listing something like this:
<PRE><CODE>Embedded Script test:
SCRIPT element: [object HTMLScriptElement]
TYPE attribute: text/javascript (specified)
parent element: [object HTMLHeadElement]
outerHTML property: &lt;SCRIPT TYPE="text/javascript"&gt;alert('Dynamically created embedded script has been parsed');&lt;/SCRIPT;&gt;
innerHTML property: alert('Dynamically created embedded script has been parsed');
text property: alert('Dynamically created embedded script has been parsed');
normalised textual contents: alert('Dynamically created embedded script has been parsed');</CODE></PRE>
<P>David "liorean" Andersson, 2004So, instead of placing Text nodes within the script elements using appendChild, change it's text property to the appropriate text.

06-01-2004, 07:14 PM
It worked, sadly in my case it doesn't as my script needs to call a function giving as an argument a reference of a textarea that just few lines above has been introduced.

Mozilla (again) does the job, but ie not, i've heard that it is asyncronous and then the reference i pass to the function cannot be read because ie has not still put inside.

And i cannot use the window.onload because all this stuff runs inside a function.

So, first thing i have done is to call the funcion directly instead of adding the script tag inside (i really didn't have to!!).
Next has been to write the function called (in an outside .js file) to the same .js file, and then to use directly the reference returned when created the node.

You could ask why not send that reference to the function, but message is the same "reference is null".

So summarizing, my initial question (about the script tag) has been solved, so thanks thousands.

Then the other problem lies on ... hell, i'm just working on it!!

06-01-2004, 07:21 PM
Finally, the last problem (of mine) was false ... so my solution has been eliminating the tag script i used to call a function and instead to call the function directly.

Anyway i've tested your solution and it has worked so for a next time i need that functionality i (and everyone that search google) knows it.
So thanks a lot!!

07-21-2006, 12:00 PM
I had the same problem and looking for the solution in google this is the first link I have found. Though I see the date of the last post is two years ago, I write the solution I found to save it in Internet.

Internet Explorer doesn't allow to add child nodes to scripts tags, for that reason it doesn't work "appendChild", "innerHTML", etc. The solution is to use "text" property. It works in Internet Explorer and Firefox:

var head = document.getElementsByTagName("head");
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "alert('hello world');";