Javascript / JQuery - redirect on window resize if at certain size

javascript, jquery, redirect

Solution

These are jQuery functions, so you have to wrap `window` in a jQuery object and call the functions on that: `$(window).height()` and `$(window).width()`. Also, you don't need the `px`, because these functions return only a number.

$(window).resize(function() {
    if (($(window).width() > 225) && ($(window).width() < 255) && ($(window).height() > 330) && ($(window).height() < 400))
    {
        window.location = "URL GOES HERE"
    };
});

You could save them in a variable so you don't need to query them twice.

$(window).resize(function() {
    var w = $(window).width();
    var h = $(window).height();
    if ((w > 225) && (w < 255) && (h > 330) && (h < 400)) {
        window.location = "URL GOES HERE";
    }
});

As @tdammers suggested in a comment under your question, there must be a better solution to your problem than this.

Problem

I'm trying to write some script which will redirect when the user resizes the screen to meet a certain threshold. I'm using the JQuery window resize function and the code I've written is as follows: ``` $(window).resize(function(){ if ((window.width > 225px) && (window.width < 255px) && (window.height > 330px) && (window.height < 400px)) { window.location = "URL GOES HERE" }; ```

Original source