HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
create <script> element problem in IE
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML
View previous topic :: View next topic  
Author Message
wakan



Joined: 20 Jan 2005
Posts: 1

PostPosted: Thu Jan 20, 2005 4:26 am     create <script> element problem in IE Reply with quote

Hi,
I'm creating a script tag in this way:
Code:

var text   = d.createTextNode("show_AS("+row_num+");");
var script   = d.createElement("script"); script.appendChild(text);

where row_num is a counter...(I'm dinamically adding rows to a table)
in Firefox, this works fine...(I have other big problem in Firefox, but I'll hope to ask later this question of the script tag)
in IE it doesn't work: it says that I can't append text to the script element...
any answer?
thanks in advance
Ciao
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Tue Jan 25, 2005 12:43 am     For IE, use 'text' property of script nodes. Reply with quote

IE doesn't allow script tags to have children; the 'text' property can be used instead. Try this:
Code:
/*...*/
function appendChild(node, text) {
  if (null == node.canHaveChildren || node.canHaveChildren) {
    node.appendChild(document.createTextNode(text));
  } else {
    node.text = text;
  }
}
/*...*/
var script = document.createElement("script");
script.setAttribute('type','text/javascript');
appendChild(script, "show_AS("+row_num+");");


As long as the non-method appendChild is only called on script nodes, you can omit the test of node.canHaveChildren, as it will always be false. I don't know if you're setting the 'type' attribute of the script elsewhere; if not, add that step as above.

Out of curiosity, why do you need to dynamically create the script?
thamizh



Joined: 06 Oct 2007
Posts: 1

PostPosted: Sat Oct 06, 2007 1:29 am     Not working Reply with quote

Hi,

I tried with your way. but still IE is having some problem. It did not accept node.text = scriptContent;

My IE crashed once that particular line is executed.

But in firefox dynamic <script> element creation is working.

I need your answer. this is very urgent. can you help me out?

thanks

- thamizharasu S
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Mon Oct 08, 2007 1:46 pm     Need more info Reply with quote

What version of IE are you using? What is the source for the script you're using?

Try this (complete) example and tell me if it fails and with what error:
Code:
<html>
<body>
<script>
function show_AS(row_num) {
  document.writeln("show_AS("+row_num+")<br>");
}

function appendChild(node, text) {
  if (null == node.canHaveChildren || node.canHaveChildren) {
    node.appendChild(document.createTextNode(text));
  } else {
    node.text = text;
  }
}
/*...*/

var script = document.createElement("script");
script.setAttribute('type','text/javascript');
appendChild(script, "show_AS(1);");

document.body.appendChild(script);

</script>
</body>
</html>


Also think about alternatives to a dynamically created script element. Could you accomplish the same task using (e.g.) events?
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Webmaster Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial