4 Replies - 4408 Views - Last Post: 29 January 2013 - 06:03 AM

#1 Pingoo1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 13

Javascript not working in IE8

Posted 29 January 2013 - 05:14 AM

This problem has been driving me nuts for the last 48 hours.

I am new to developing websites in HTML/CSS/Javascript. I come from a Actionscript background so learning how to code in each of those languages is not a massive challenge. I am half way through coding my new online portfolio and I am using Javascript with the Greensock TweenMax library to take care of all the fancy animations.

That said, everything works very well in Chrome and FF but when I try open the website in IE8, locally or not, it stops at the very first line of my Javascript code and the page simply will not work.

The first line in my JS is used to trigger a function (init) once the DOM has loaded and goes something like this:

document.addEventListener('DOMContentLoaded', init, false);

which I have changed to:

document.attachEvent('DOMContentLoaded', init, false);

in order for it to work in IE8 but with no luck.

I am stuck there. IE is ignoring all the rest of my lovely JS code because of that error. Any ideas?

I am surprised that so many people are talking about the demise of Flash when coding websites in HTML requires one to work around all the silly exceptions demanded by each browser!!

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript not working in IE8

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3807
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Javascript not working in IE8

Posted 29 January 2013 - 05:31 AM

document.attachEvent('DOMContentLoaded', init, false);

attachEvent uses 'on' and only has two arguments:

document.attachEvent('onDOMContentLoaded', init);

but DOMContentLoaded is only supported from IE9

MDN

Quote

Internet Explorer 8 supports the readystatechange event, which can be used to detect that the DOM is ready. In earlier version of Internet Explorer, this state can be detected by regularily trying to execute document.documentElement.doScroll("left");, as this snippet will throw an error until the DOM is ready.


but I would question why you need to use this? Just put your Javascript code at the bottom of the page, before the closing body tag, and you won't need to attach an event.

This post has been edited by andrewsw: 29 January 2013 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Javascript not working in IE8

Posted 29 January 2013 - 05:32 AM

Hey.

The problem is that IE8 is too old to support the DOMContentLoaded event. You'll have to fall back on the attachEvent("onload", ...) event in IE8.

Unless you very much feel like handling all these type of IE/cross-browser incompatibilities, I very much recommend utilizing a Javascript library like jQuery for the event handling and animations. They will provide an alternate, cross-browser compatible way of declaring events. So, for example, using jQuery, you would do this to trigger code as soon as the page is ready:
$(function() {
    // DOM is ready in here!
});



Likewise, you can attach events in a cross-browser compatible way like this:
$("button.myAction").on("click", function() {
    // Button has been clicked!
});


This will attach a "click" handler to all <button> elements that have a "myAction" class set on it.

Quote

I am surprised that so many people are talking about the demise of Flash when coding websites in HTML requires one to work around all the silly exceptions demanded by each browser!!

These "silly exceptions" are usually just for old IE versions, and most of them can easily be avoided using things like jQuery. The "modern" browsers (read: browsers supporting modern techniques), like Firefox and Chrome, tend to be compatible in almost all things. (Not that there aren't a few exceptions, but they are relatively rare.)

Either way, HTML5 - which is what people like to think is going to kill Flash, though I personally think that is highly optimistic - is only available at a resonable level in the "modern" browsers, so if you are going to use those features, you wouldn't need to worry about these types of issues anyway. IE8 or 9 would never be capable of running most of the "Flash killing" code.
Was This Post Helpful? 0
  • +
  • -

#4 Pingoo1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 13

Re: Javascript not working in IE8

Posted 29 January 2013 - 05:39 AM

Thanks for the quick replies! I will have a go at implementing your suggestions. I never use IE but I know that a lot of agents do when checking out freelancers portfolios. God only knows why!
Was This Post Helpful? 0
  • +
  • -

#5 Pingoo1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 13

Re: Javascript not working in IE8

Posted 29 January 2013 - 06:03 AM

Thanks, using jquery seems to do the trick!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1