Jump to content
Sign in to follow this  

Javascript Error Only On Ie?

Recommended Posts

I am writing a code that will generate the contents of a calendar into a table.For some reason, this code seems to work on all browsers except for IE which gives me an error of:

Line: 41Error: 'this.temp2.firstChild' is null or not an object
Here is the piece of code:
window.onload = constructor1;var cdate = new Date();var cday = cdate.getDate();var cmonth = cdate.getMonth();var cyear = cdate.getFullYear();var Month = cmonth;var Year = cyear;var calendar = document.createElement("table");calendar.id = "calendar";function constructor1(){	var doc = document;	var txtNode = doc.createTextNode("");	//creates the skeleton of the calendar	var tBody = doc.createElement("tbody");	var temp1 = tBody.insertRow(0);	var temp2 = temp1.insertCell(0);	temp2.appendChild(txtNode);	for (var c=6; c--;) temp1.appendChild(temp2.cloneNode(true));	for (var r=5; r--;) tBody.appendChild(temp1.cloneNode(true));	//finds and assigns the date values on the calendar	var cMonth = cmonth;	var cYear = cyear;	var pMonth, pYear;	var daysCMonth = 32 - new Date(cYear, cMonth, 32).getDate();	var counter = [		42 - new Date(cYear, cMonth, 1).getDay() - daysCMonth,		daysCMonth,		32 - new Date((cMonth)?cYear:--cYear, (cMonth)?--cMonth:11, 32).getDate()	];	var i = 0;	for (var r=6; r--;) {		temp1 = tBody.rows[r];		for (var c=7; c--;) {			temp2 = temp1.cells[c];			temp2.firstChild.nodeValue = counter[i]--;	 //<--- line of error			if (i & 1); else temp2.id = "oMonth";			if (counter[i]); else ++i;		}	}	//puts the calendar on the page	calendar.appendChild(tBody);	doc.body.appendChild(calendar);//	doc = txtNode = tBody = cMonth = cYear = daysCMonth = temp1 = temp2 = i = null;}

As far as i know, i haven't use code needs cross browser handling.Unless there is something i have been doing wrong. :)Edit: Nevermind, i found the source of error. It was caused by an IE bug when trying to clone text nodes. IE doesn't clone text nodes that has empty strings so i had to initialize the text node with some values. Really, IE is such a pain specially when it is not following standards. :)

Edited by banzemanga

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...