...

View Full Version : [js noob] appendChild problems in ie



kyleschrama
05-25-2006, 04:23 AM
i'm trying to generate a calendar using dom to create a table. it works in firefox and opera, but ie won't show it, and sometimes gives me an alert saying it can't display the page, and then shows a 404 page. here's the code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>testing calendar generation</title>
<script type="text/javascript">
function printcal(){
var calItem = document.getElementById('dynamicTable');
for(var i=0;i<5;i++){
var tablerow = document.createElement("tr");
for(var j=0;j<7;j++){
var tablecell = document.createElement("td");
var celldata = document.createTextNode(i+''+j);
tablecell.appendChild(celldata);
tablerow.appendChild(tablecell);
}
calItem.appendChild(tablerow);
}
}
</script>
</head>
<body>
<p>Senectus vel accumsan eleifend in laoreet interdum orci nec vitae Sed.
Wisi felis Integer justo Suspendisse euismod et ornare laoreet tempus Morbi.
Dignissim sed libero Lorem congue ut ipsum In id et leo. Ut amet neque hac
Nam lacinia cursus non Nunc a congue. Nibh Phasellus Sed enim vel nec.</p>
<table id='dynamicTable'>
<script type="text/javascript">
printcal();
</script></table>
<p>Sit tellus facilisis pulvinar congue In Nam ut facilisis est pede. Sem Sed
leo accumsan velit orci eget Vivamus Curabitur cursus et. Turpis id hac tempus
et mauris suscipit nulla id id eu. Tempor dui lacus nibh sapien tellus
semper egestas a Sed tempus. Habitant accumsan Vestibulum eget ut Nam
leo pretium vitae gravida fringilla. Tellus nulla vel hendrerit interdum
Aenean laoreet quis dui adipiscing Sed. Vivamus.</p>

</body>
</html>



i want it to write a table into the dynamictable element. any suggestions? thanks

Kravvitz
05-25-2006, 05:38 AM
It would be better if you used a window.onload event handler function to call printcal() instead of placing that script element in the table like that.

I suspect that you need to append the rows to the <tbody> instead of the <table>.

After

var calItem = document.getElementById('dynamicTable');
add

if(calItem.getElementsByTagName && calItem.getElementsByTagName('tbody').length) {
calItem = calItem.getElementsByTagName('tbody')[0];
}

kyleschrama
05-25-2006, 07:10 AM
yup, i just needed a tbody for the rows to go in. i found that in another thread on here just a few minutes ago too.

thanks for the tip about window.onload, hadn't thought about using that.

Beagle
06-02-2006, 11:31 PM
window.onload and appendChild and IE have some problems. On complex pages that take a while for certain things to happen, you can actually cause a naster bug that will stop IE from loading the page until you restart the browser.

You might find it better to use window.onreadystatechange if you run into this problem. I would try it out, and if you can get the onreadystatechange to work use it, because users with slower machines could run into problems when you try to appendChild if IE isn't ready.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum