For some reason the following code does not work as expected when in IE 8, as noted in the comment. I'm trying to add a "writeSpecial" method to the document object, and this works fine the first time that the method is called, but whenever this is called a subsequent time, it becomes undefined.
However, this only happens in IE when I use the onload event. There seems to be no difference between an inline body onload and window.attachEvent('onload', main);. If I call main() directly from the script block, it works fine. Again, this is only in IE.
Would anyone happen to know why this is?
Thanks!
<html>
<head>
<title>Test</title>
<script type="text/javascript">
document.writeSpecial = function(str)
{
this.write(str + " [specialfied]");
}
function main()
{
alert(document.writeSpecial);
document.writeSpecial('test 1');
alert(document.writeSpecial); //document.writeSpecial is undefined here in IE, works in firefox...why?
document.writeSpecial('test 2');
}
</script>
</head>
<body onload="main()">
</body>
</html>