Jump to content

DOCTYPE declaration destroys getElementById


toxicious
 Share

Recommended Posts

Everything worked perfectly everywhere, except for in IE. I found out that I needed a DOCTYPE declaration at top to make the page show correctly in IE. So I added this one:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Now the page looks as it should in all browsers, except for one thing, that line of code broke my javascript resize of some divs at page load?!I have hooked the jquery window.load function to resize 2 divs at the page, but it doesn't work anymore. If I remove the DOCTYPE it works again. So I figured there must be something "invalid" in my resize code, which is this one:

function resize(){	    var pageWidth = document.body.offsetWidth;var middleWidth = 530;var sideWidth = (pageWidth - middleWidth) / 2;document.getElementById("leftCol").style.width = sideWidth;document.getElementById("rightCol").style.width = sideWidth;}

In the error console it says that "document.getElementById("leftCol") is null". Why can't I use getElementById anymore? :o

Link to comment
Share on other sites

It might be due to case sensitivity. Check that your element's ID is "leftCol" and not "leftcol" or something else. Also, be sure the element has an "id" attribute and not a "name" attribute. The DOCTYPE only breaks the page if something was wrong in it to begin with.

Link to comment
Share on other sites

I'm not entirely sure, but depending on what IE your checking the results with, if it's ie8, then it may be because of what's described here: http://yyosifov.blogspot.com/2009/03/getelementbyid-compared-in-ie6-ie7-and.html Maybe not, but I'd thought post this because I came across a similar problem not too long ago and the site helped me understand what was going on.

Link to comment
Share on other sites

I'm not entirely sure, but depending on what IE your checking the results with, if it's ie8, then it may be because of what's described here: http://yyosifov.blog...e6-ie7-and.html Maybe not, but I'd thought post this because I came across a similar problem not too long ago and the site helped me understand what was going on.
Thanks for the tip, but I tested it in IE, Chrome and Firefox, so nope, not because of that. Edited by toxicious
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...