Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  151] [ 1]  / answers: 1 / hits: 17999  / 10 Years ago, thu, april 17, 2014, 12:00:00

I don't know why this doesn't work:



window.addEventListener('load', setSize(), false);
window.addEventListener('resize', setSize(), false);

function setSize(){
width = window.innerWidth;
console.log(width);
}


It logs the width right after load, but it doesn't do that when I resize.


More From » html

 Answers
24

Instead of executing the function (and passing the result) :



window.addEventListener('load', setSize(), false);
window.addEventListener('resize', setSize(), false);


you should pass a reference to the function:



window.addEventListener('load', setSize, false);
window.addEventListener('resize', setSize, false);


The function will execute when the event listener triggers.


[#71423] Tuesday, April 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rayvenc

Total Points: 666
Total Questions: 125
Total Answers: 99

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
;