简体   繁体   中英

window.resize not working when creating it in a function

Why, in the console does this display the browser size after every size change (as expected):

window.onresize = function() {
        var width = $(window).width(); 

        console.log(width);
}

http://jsfiddle.net/5r35gLrc/

But when creating it as a function and calling it, it does not?:

window.onresize = browserResizeAdjustments();

function browserResizeAdjustments() {
    var width = $(window).width(); 

    console.log(width);

}

http://jsfiddle.net/wxx8do60/

The reason being is I want to run this onload as well, not just when the first browser size change happens.

Final Solution:

<script type="text/javascript">
window.onresize = browserResizeAdjustments;

function browserResizeAdjustments() {
    var width = $(window).width(); 

    console.log(width);

}

// This runs it onload
browserResizeAdjustments();
</script>

window.onresize should be a reference to a function, not the result of a function call. Try this:

window.onresize = browserResizeAdjustments;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM