Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php echo statement if screen is a certain size

I have a series of jQuery scripts on my site that I want to only run if the users screen width is greater than 960px. I know that you can't detect screen size using php but is there a way to create something to this effect:

<? php 
if [METHOD TO DETECT SCREEN SIZE] > 960px {
echo '<script src="js/nbw-parallax.js" type="text/javascript"></script>';
}
?>
like image 538
Sam Skirrow Avatar asked Jan 18 '26 08:01

Sam Skirrow


2 Answers

PHP is server side and can't grab your screen width and height.

You have to use javascript.

JQuery

if( $(window).width() > 960 ) {
     $.getScript('js/nbw-parallax.js');
}

JavaScript

if( window.innerWidth > 960 ) {
    //Your Code
}
like image 184
Jordi Kroon Avatar answered Jan 19 '26 21:01

Jordi Kroon


Why not use jQuery?

if( $(window).width() > 960 )
{
  $.ajax({
    url: 'js/nbw-parallax.js',
    dataType: "script",
    success: function() {
        //success
    }
  });
}
like image 42
Repox Avatar answered Jan 19 '26 21:01

Repox