2

EDIT : THe problem is that the site is sooo bad, and he is still in document.readyState = "interactive" so I don't have resize events :'(

I'm working on a website (that I didn't made). I'm trying to integrate a banner properly. I face a problem with IE 10/9. The even resize is not working.

this is the code i use

function addEvent(elementDOM, evenement, fonction){
    var refEvent = null;
    if(elementDOM)
    {
        if (elementDOM.addEventListener)
        {
            refEvent = fonction;
            elementDOM.addEventListener (evenement,refEvent,false);
        }
        else if (elementDOM.attachEvent)
        {
            refEvent =function() {
                return fonction.call(elementDOM,window.event);
            };
            elementDOM.attachEvent ('on'+evenement,refEvent);
        }
        else
        {
            elementDOM["on"+evenement] = fonction;
        }
    }
    return refEvent;
}

I call it like that :

addEvent(window,"resize",function(){
        console.log("a");
    });

And this doesn't works on IE 10 and 9 (it works on FF and Chrome) I enter in the AddEventListener if, but no events are fired. I never see my console log ("a")

the website is this one

http://www.courrierinternational.com/

Can someone help me cause I'm stuck... I really don't know what could be the problem.

Thx :3

Community
  • 1
  • 1
Bobby
  • 4,372
  • 8
  • 47
  • 103
  • Probably the `attachEvent` is busted somehow. This is what older versions of IE use. The code looks a little weird but I think it should work, but I guess it doesn't? – Halcyon May 21 '15 at 12:38
  • Hi! I tryed to add the event from the console by using addEventListener, AttachEvent and also onresize and it doesn't work. Also I already used this code and it always worked, but not on this site, and I don't get why – Bobby May 21 '15 at 12:43
  • Can you verify that the `refEvent` function is called? – Halcyon May 21 '15 at 12:45
  • Are there any errors in the javascript console? – greenhoorn May 21 '15 at 13:44
  • The problem is that the site is still in document.readyState = "interactive".... – Bobby May 21 '15 at 13:58

1 Answers1

0

I have no idea how to create something with document.readyState = "interactive" so I've not been able to duplicate this.

That said, you can set up a function to watch for the document being resized and manually triggering an event yourself.

Something like:

// Warning, code written on-the-fly and untested
(function() {
  var oldWidth;
  var oldHeight;

  function watchForResize() {
    // from http://stackoverflow.com/questions/1248081/get-the-browser-viewport-dimensions-with-javascript
    var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
    var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)

    if ((w !== oldWidth) || (h !== oldHeight)) {
      window.dispatchEvent(new Event('resize'));
    }

    w = oldWidth;
    h = oldHeight;

    window.setTimeout(watchForResize, 100);
  }
}());
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74