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"
};
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"
};
Share
Improve this question
edited Apr 30, 2012 at 9:03
kapa
78.7k21 gold badges165 silver badges178 bronze badges
asked Apr 29, 2012 at 18:39
Jack WildJack Wild
2,1426 gold badges28 silver badges40 bronze badges
1
- 4 This sounds like a really bad idea to me. I can easily imagine users getting suspicious when the browser suddenly starts reloading the page out of the blue. What is the exact problem you are trying to solve here? It's quite likely that it can be solved more elegantly (and less obtrusively) using CSS media queries (and maybe a little help from JS, but not redirecting or reloading). – tdammers Commented Apr 29, 2012 at 18:48
1 Answer
Reset to default 7These 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 ment under your question, there must be a better solution to your problem than this.