1

I have this problem, I need script should work in live time when I resize browser window less than 340px alert window is less than 340px and it should happened only one time when $(window) size is less then 340px or more 340px it should be every resize less then 340px or more

if($(window) < 340){
     alert("window is less than 340px");
    }else{
    alert("123");
    }
Val Do
  • 2,695
  • 4
  • 20
  • 35

3 Answers3

2

You need to make this check whenever the window is resized. For that, there is the resize event.

You also need to test the width of the window, you can get this using the .width() method.

I've changed alert() for console.log() since this functionality will be invoked every time the cursor moves even a pixel whilst resizing. alert() will remove your cursor focus.

I've also triggered the resize event after attaching an event handler to have this function run the initial page load.

$(window).resize(function(){
    var $win = $(this);
    if($win.width() < 340){
        console.log("window is less than 340px");
    } else {
        console.log("123");
    }
}).resize();
George
  • 36,413
  • 9
  • 66
  • 103
  • I need resize happened only one time if window size is less than 340px and if more than 340px `alert("some text")` and this function should happen every time when window is less then 340px or more then 340px – Val Do Nov 20 '14 at 12:51
  • Be aware to check width, it is usually better to use `window.matchMedia` (&polyfill) or to check using e.g: http://stackoverflow.com/a/21351445/1414562 – A. Wolff Nov 20 '14 at 12:53
2
$(window).resize(function(){
if($( window ).width() < 340){
alert("window is less than 340px");
}else{
console.log('more 340px');
}
});
Kristiyan
  • 1,655
  • 14
  • 17
2

Just use the Jquery $(window).resize() function

$(window).resize(function(){
    if($(window).width() < 340){
         alert("window is less than 340px");
    }
    else{
        alert("123");
    }
});