Crocsx
Crocsx

Reputation: 7640

Add Event Listener not working

I have a problem that i never had before.

I wanna call a function when a mobile device change is orientation, so i'm using "onorientationchange" and to be sure it's triggered, i also use "onresize".

The problem is that the even don't work on the page for a mysterious reason :

if i do

document.body.addEventListener("resize",function(){console.log("here")};

it doesn't work, I so created a custom version of AddEventListener :

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

And even this one don't work...

BUT this works :

document.body.onresize = function(){console.log("here")};

it's a normal webpage, i don't get why it doesn't work.

Can you help? :x

Upvotes: 0

Views: 364

Answers (1)

Quentin
Quentin

Reputation: 944546

resize events fire on the window object, not the body element.

You're listening for it in the wrong place.

window.addEventListener("resize",function(e){console.log("here", e.target)};

Upvotes: 5

Related Questions