-1

is there a way to code this so it will work for IE8 and up, I am trying to find a JQuery solution avoid a check on the browser... (addEventListener does not work in IE8)

document.addEventListener('readystatechange', function () { alert("foo"); })
user80855
  • 1,394
  • 2
  • 11
  • 21

2 Answers2

1

Here's a simple cross browser event function that will work in versions of IE back to IE6 and in all other browsers:

// add event cross browser
function addEvent(elem, event, fn) {
    if (elem.addEventListener) {
        elem.addEventListener(event, fn, false);
    } else {
        elem.attachEvent("on" + event, function() {
            // set the this pointer same as addEventListener when fn is called
            return(fn.call(elem, window.event));   
        });
    }
}

addEvent(document, 'readystatechange', function() {
    alert("got it!");
});

Of course, if you already have jQuery, you can just use:

$(document).ready(function() {
    alert("got it");
});

And this will automatically handle all sorts of browser versions for you.


FYI, there's a full substitute for jQuery's .ready() in plain javascript here if you're really trying to get the best DOM loaded detection without jQuery. After including that function, you would just use:

docReady(function() {
    alert("got it");
});
Community
  • 1
  • 1
jfriend00
  • 580,699
  • 78
  • 809
  • 825
1

If you're using jQuery, you shouldn't need to call addEventListener directly. Use on:

$(document).on("readystatechange", function() {
    alert("foo");
});

jQuery handles all the browser dependencies for you, that's one of the reasons we use it.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Why use this rather than `$(document).ready()` because `.ready()` has more advanced logic in it than just listening for `readystatechange` if you just want to know when the document is ready? If you're doing your own doc ready logic with readystatechange, you should be aware of all the browser bugs that jQuery has already designed around in the `.ready()` logic. – jfriend00 Jun 20 '14 at 01:04
  • I didn't want to second-guess what the OP is trying to do. I just wanted to show how to translate his code to jQuery. – Barmar Jun 20 '14 at 01:09
  • @user80855 - And, I'm trying to think at a bit higher level (something I try to do on SO rather than only answer what is asked because often the OP doesn't ask for the best solution) because I can think of no reason to try to reinvent a lot of the logic that's built in jQuery's `.ready()` by implementing your own `readystatechange` listener. My comment was really directed at the OP. – jfriend00 Jun 20 '14 at 01:13
  • I also didn't think the OP was asking only about the `readystatechange` event. I thought he wanted to know how to bind event handlers in general. – Barmar Jun 20 '14 at 01:22
  • Could be. Odd to be using jQuery and be asking about cross browser event handling. That seems like the very first thing you use jQuery for. But, maybe you're right. The question was certainly ambiguous in that regard. The jQuery tag is the only clue that perhaps that's what they're really asking. – jfriend00 Jun 20 '14 at 01:24
  • @jfriend00 See my first comment to the question, I thought it was odd, too. – Barmar Jun 20 '14 at 01:28