1

I'm trying to execute a javascript function ONLY if the width is bigger than 1200pixels.

The code I need to execute works, but the check for the screen size doesn't:

window.addEventListener("resize", function(){
    if (document.documentElement.clientWidth > 1203) {
            <<<some other code that's working>>>
    }
}, true);

All this code is in a javascript file outside the document.ready function.

EDIT: When you drag your screen window around, it isn't working like normal css queries. This is the problem. It works if you refresh your page, but if you change the width of your browser, then it doesn't adjust, it needs the reload right now.

That's my issue.

7
  • 1
    Code seems to work fine for me... Do you want it to also fire on load? Because this will only work when the window is resized. Commented Feb 19, 2015 at 13:10
  • @Jivings When I resize my screen it doesn't work atm, I need to manually reload the page in order for the code to check for the screen size. Commented Feb 19, 2015 at 13:13
  • what browser/version are you seeing this in? Commented Feb 19, 2015 at 13:15
  • I just found out that it works when I start from mobile size and resize bigger, but when I start desktop and resize smaller, it doesn't re-update without reloading. Commented Feb 19, 2015 at 13:18
  • Here's an example, which works fine in Chrome 40: jsfiddle.net/9n5hmpua/embedded/result Commented Feb 19, 2015 at 13:19

3 Answers 3

3

You need to add an else clause to get the behaviour you want. Otherwise the resize event will only work going one way, as you described.

window.addEventListener("resize", function(){
    // fire when above 1203
    if (document.documentElement.clientWidth > 1203) {
      console.log('Greater!');
    }
    // fire when below 1203
    else {
      console.log('Smaller!');
    }
}, true);

Here's a link to the fixed jsfiddle that planet260 wrote: http://jsfiddle.net/4dnemh2j/4/

Here's my example again: https://jsfiddle.net/9n5hmpua

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all your trouble, I think maybe my problem is somewhere else then :/ Here's my full code: pastebin.com/W3F9HK8F perhaps you see my mistake?
0

The problem is the true you're passing to addEventListener.

See the useCapture section here: EventTarget.addEventListener

Events which are bubbling upward through the tree will not trigger a listener designated to use capture

So you want:

window.addEventListener("resize", function(){
    if (document.documentElement.clientWidth > 1203) {
        <<<some other code that's working>>>
    }
}, false);  <----change here

2 Comments

I changed it to false, but the issue is still here :/
But the event should be firing directly on window, no bubbling here.
0

You can use jQuery to achieve this

$( window ).resize(function() {
  console.log($( window ).width());
  var windowwidth = $( window ).width();
  if (windowwidth > 500) {
      console.log("Greater than 500");
     /*Do your work here*/ 
  }
});

1 Comment

This works, but only when I go from small to big (mobile to desktop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.