Problems of inserting javascript dynamically under script tag in IE

321 views
Skip to first unread message

Renee Lau

unread,
Nov 21, 2010, 11:06:22 PM11/21/10
to Google Web Toolkit
Hi

I have problem on inserting javascript under the scirpt tag.
I am doing a kind of dynamic form which allow users to insert some
simple javascript which associated with widget events.

My problem is that the code works fine in FF and Chrome but not in IE
browsers and after some investigation.
It is found that IE is not allowed some javascript features to be run
on its browser which will cause memory leakage.

My code is very simple and shown as below:

1. Document document = Document.get();
2. script = document.createElement("script");
3. script.setAttribute("type", "text/javascript");
4. script.setAttribute("id", "custom_javascript");
5. script.appendChild(document.createTextNode(javascripSource));
6.
document.getElementsByTagName("head").getItem(0).appendChild(script);

The error message caused by line 5 is:
Unexpected call to method or property access.

It is because IE does not allowed appendChild in script tag

so i changed to use innerText/innerHtml
replace line 5 to script.setInnerHTML(javaScriptSrc);

The error message is Unknown runtime error.

No matter i wrote the code in GWT or use the native method to do it in
javascript way. it fails.

I have search the forum and google it for days but still no luck.
Can anyone give me some ideas on how to fix it in IE browsers?

Thanks in advance.

Renee


Bryan

unread,
Nov 22, 2010, 12:43:17 PM11/22/10
to Google Web Toolkit
Instead of doc.createElement("script") you can use
doc.createScriptElelment();

final Document doc = Document.get();
final ScriptElement script = doc.createScriptElement();
script.setType("text/javascript");
script.setText(strJavascript);

Renee Lau

unread,
Nov 22, 2010, 8:10:50 PM11/22/10
to Google Web Toolkit
Thanks for your reply, Bryan.

Thanks! It works like a charm!

Renee
Reply all
Reply to author
Forward
0 new messages