Sam Skirrow
Sam Skirrow

Reputation: 3697

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>';
}
?>

Upvotes: 2

Views: 47086

Answers (6)

Jordi Kroon
Jordi Kroon

Reputation: 2597

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
}

Upvotes: 12

Repox
Repox

Reputation: 15476

Why not use jQuery?

if( $(window).width() > 960 )
{
  $.ajax({
    url: 'js/nbw-parallax.js',
    dataType: "script",
    success: function() {
        //success
    }
  });
}

Upvotes: 3

davidcondrey
davidcondrey

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

Patrick James McDougle
Patrick James McDougle

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

Kirill Kulakov
Kirill Kulakov

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

Nick Andriopoulos
Nick Andriopoulos

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

Related Questions