11

Trying to grab the screen size and add an if / else query, but for some reason I can't get it to work

What I'm looking for is if the screen width is larger / equal 480px, the following should be outputted:

{$pPage = $cWidth}

Else this:

{$pPage = 1}

This is what I have so far:

<script type="text/javascript">
(function($) {
    $(document).ready(function() {    
        {$pPage = $cWidth}    
})(jQuery);
</script>

Anybody out there with advice how to add the the query in reference to the screen.width, so that it also works?

Manga
  • 111
  • 1
  • 1
  • 3

1 Answers1

35

try this:

using jquery:

if ($(window).width() < 1280) {
   alert('Less than 1280');
}
else {
   alert('More than 1280');
}

OR

using javascript:

var screensize = document.documentElement.clientWidth;
if (screensize  < 1280) {
   alert('Less than 1280');
}
else {
   alert('More than 1280');
}
user2232273
  • 4,898
  • 15
  • 49
  • 75
  • Many thx, jquery version works. Amending it however to my needs doesn't work. Keeps returning the last defined value. Any idea why? `if ($(window).width() < 1280) { {$pPage = 1}; } else { {$pPage = 3}; }` – Manga Jan 25 '14 at 22:13
  • ternary! `var $pPage = ($(window).width() < 1280) ? 1 : 3;` - might also need to wrap in resize hook: `$(window).resize(function () {var $pPage = ($(window).width() < 1280) ? 1 : 3; doSomethingWith($pPage);})` – TomFuertes Mar 29 '16 at 06:52