PDA

View Full Version : Weird runtime error in IE



mortalgos
07-18-2007, 08:01 PM
Hey I have this one line of javascript that works fine in FF but creates a runtime error in IE, iv gone over it over and over and can't figure out whats causing the runtime error, any help would be great, thx


var theString = '<br /><div style="border-top:1px solid white;border-bottom:1px solid white;"><br /><textarea id="commentEditor'+target+'" style="width:383px;" onkeyup="commentChanged('+target+');"></textarea><br /><a href="javascript:saveNewComment('+target+');">Save Changes</a> | <a href="javascript:cancelNewComment('+target+');">Cancel</a></div>';

i narrowed things down a bit to try and figure it out and I have it narrowed down to one tiny part of the string:

this line of code works fine:

var theString = '<br /><div style="border-top:1px solid white;border-bottom:1px solid white;"><br /><textarea id="commentEditor'+target+'" style="width:383px;" onkeyup="commentChanged('+target+');"></textarea><br />Save Changes</div>';

but if i add in the <a> tag around save changes IE spits out the runtime error:

var theString = '<br /><div style="border-top:1px solid white;border-bottom:1px solid white;"><br /><textarea id="commentEditor'+target+'" style="width:383px;" onkeyup="commentChanged('+target+');"></textarea><br /><a href="#">Save Changes</a></div>';

this seems REALY weird to me :S thx in advance for any insight

GreenZombie178
07-18-2007, 09:01 PM
How are you using the string?

mortalgos
07-18-2007, 09:03 PM
setting it as the innerHTML of a div element

GreenZombie178
07-19-2007, 03:04 AM
can't help you, sorry.

Trinithis
07-19-2007, 03:32 AM
It works for me in IE6. Perhaps the function commentChanged() is bad.

If it still does not work for you, I have 2 suggestions. The first is to escape all the "/" by making them "\/"

The second option is to use the DOM methods instead of innerHTML, which is actually the formal way of doing it.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Test</title>
</head>

<body>
<div>

<div id="theDiv">abc</div>

<script type="text/javascript">

var target = "theTarget";
function commentChanged(foo) {return foo+"bar";}

var p = document.getElementById("theDiv");
while(p.hasChildNodes()) p.removeChild(p.firstChild);

var d = document.createElement("div");
d.style.borderTop = "1px solid white";
d.style.borderBottom = "1px solid white";

var t = document.createElement("textarea");
t.id = "commentEditor" + target;
t.style.width = "383px";
t.onkeyup = "commentChanged(" + target + ");";

var a = document.createElement("a");
a.href = "#";
a.appendChild(document.createTextNode("Save Changes"));

d.appendChild(document.createElement("br"));
d.appendChild(t);
d.appendChild(document.createElement("br"));
d.appendChild(a);

p.appendChild(document.createElement("br"));
p.appendChild(d);

</script>

</div>
</body>
</html>

Twey
07-19-2007, 07:16 AM
t.onkeyup = "commentChanged(" + target + ");";Uh-uh:
t.onkeyup = function() {
commentChanged(target);
};... and set
d = t = a = p = nullat the end to avoid memory leaks in IE.

mortalgos
07-19-2007, 11:57 AM
Hey guys, I figured it out, but it actualy wasnt a javascript error, just for some reason IE was telling me it was... what actualy was happening is way up in the code there was a </ a> instead of </a> and IE was erroring when it tried to put an anchor inside an anchor... why it was telling me it was a javascript runtime error is beyond me...

thx for all your help though

jscheuer1
07-19-2007, 05:01 PM
Hey guys, I figured it out, but it actualy wasnt a javascript error, just for some reason IE was telling me it was... what actualy was happening is way up in the code there was a </ a> instead of </a> and IE was erroring when it tried to put an anchor inside an anchor... why it was telling me it was a javascript runtime error is beyond me...

thx for all your help though

That's the typical IE response to such things.