Reputation: 3697
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>';
}
?>
Upvotes: 2
Views: 47086
Reputation: 2597
PHP is server side and can't grab your screen width and height.
You have to use javascript.
if( $(window).width() > 960 ) {
$.getScript('js/nbw-parallax.js');
}
if( window.innerWidth > 960 ) {
//Your Code
}
Upvotes: 12
Reputation: 15476
Why not use jQuery?
if( $(window).width() > 960 )
{
$.ajax({
url: 'js/nbw-parallax.js',
dataType: "script",
success: function() {
//success
}
});
}
Upvotes: 3
Reputation: 35983
<?php
if ( stristr($ua, "Mobile" )) {
$DEVICE_TYPE="MOBILE";
}
if (isset($DEVICE_TYPE) and $DEVICE_TYPE=="MOBILE") {
echo '<script src="js/nbw-parallax.js" type="text/javascript"></script>';
}
?>
Here's a link to a more detailed script: PHP Mobile Detect
Upvotes: 2
Reputation: 2062
You can have a javascript that sets a cookie containing the user's screen size. (Sure it won't work on the first request, but every subsequent request will work). Then in php you can get the value of the cookie.
Upvotes: 0
Reputation: 10245
you should make the JS to detect the resolution and choose by itself whether it should run some code or not.
Upvotes: 0
Reputation: 10643
you will have to use a javascript (or jQuery) function to generate the new script tags depending on the screen size. You will probably find this http://api.jquery.com/jQuery.getScript/ helpful.
Upvotes: 0