PDA

View Full Version : ie errors FF passes ... is null or not an object...


mlitty
04-20-2007, 03:07 PM
Hi.
I'm new to Dom Scripting and using Jeremy Keith's Dom Scripting book as a guide.
My code is giving me a code error in IE but working smoothly in FF, and passes the JSLint.com validator.


Below is the relevent snipet of code. It's at the very top of my script. The line that triggers the error is ...
if (templateClass.indexOf("bottom") != -1 ) {

I don't have IE at home (linux user), but I believe the error was ...

templateClass is null or Not an object


and happens at the top of the templateTweak function. It's right out of the DOM Scripting book, works in FF and errors in IE.
Thanks for helping...



var bodyTag = document.getElementsByTagName("body");
var templateId ;
var templateClass;
var displayArea; /* This is where we'll append Image and Caption code */
var description; /* This is where we'll append a caption for the display */
var descText;

window.onload = function() {
templateTweak(); /* Make adjustments based on the teplate */
makeDisplay();
/*idLinks(); uses GetElementById to find links in a particular Id'ed location" */
classLinks(); /* retrieves all of the elements of a certain class */
};

function templateTweak() {
var i=0;
var classSearch = document.getElementsByTagName("div");
templateId = bodyTag[0].getAttribute("id");
templateClass = bodyTag[0].getAttribute("class");

if (templateClass.indexOf("bottom") != -1 ) {
document.getElementById("top").style.visibility = "hidden"; // hide the top bar
for (i; i<classSearch.length; i++) {// search, find, and hide the sideBars
if (classSearch[i].getAttribute("class") == "sideBar") {
classSearch[i].style.visibility = "hidden";
}
}
}

david_kw
04-22-2007, 10:51 PM
I suspect the problem is here

templateClass = bodyTag[0].getAttribute("class");

In IE, you need to do a className search so it would look like

templateClass = bodyTag[0].getAttribute("className");

I believe that works in FF as well (either works in FF that is).

david_kw

liorean
04-22-2007, 11:35 PM
For full compatibility and standards compliance you should use bodyTag[0].className. getAttribute is broken in IE, and there is no "className" attribute on the element, so trying to use that with getAttribute is not a good idea.

mlitty
04-23-2007, 02:47 AM
Thanks to both of you.
I'm going to try the document.bodyTag[0].className option, since that seems to be the simplest and most standard. Does that also work with document.bodyTag[0].idName ?

Thanks again.

david_kw
04-23-2007, 03:55 AM
Nope, it would still be ".id" for the id. Only a few of the attributes have been renamed in IE.

david_kw