1

So with help from others I have managed to get this executed and running correctly via PHP locally however I am having problems with the corporate web-server.

How can I go about the same thing using javascript instead?

I understand this is not responsive mobile design and there are much better ways of getting this delivered.

Would appreciate the help

<?php

    // MOBILE
    $blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BB10");
    $blackberry2 = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $chrome = strpos($_SERVER['HTTP_USER_AGENT'],"Chrome");
    $safari = strpos($_SERVER['HTTP_USER_AGENT'],"Safari");

    // REDIRECTS 
    // MOBILE
    if ($blackberry == true) 
    { 
    header('Location: style.css');
    }
    else if ($blackberry2 == true)
    {
    header('Location: bb7.css');
    }
    else if ($iphone == true) 
    { 
    header('Location: style.css');
    }
    else if ($ipad == true) 
    { 
    header('Location: ipad.css');
    }
    else if ($chrome == true) 
    { 
    header('Location: style.css');
    }
    else if ($safari == true) 
    { 
    header('Location: style.css');
    }
?>
7
  • Why do you need to do it with JavaScript? Commented Apr 4, 2014 at 17:36
  • You can check the user agent in javascript using window.navigator.userAgent but I suspect this is even what you want to do in php Commented Apr 4, 2014 at 17:36
  • 1
    You know it would be kind of slow using javascript? It would load the stylesheet after everything on the page is loaded. Commented Apr 4, 2014 at 17:37
  • 2
    Also it would be better to just include the stylesheet in your .php file rather than redirecting the browser. Commented Apr 4, 2014 at 17:38
  • 1
    I simply want to serve different user agents separate stylesheets. The server I am attempting to upload to is frustrating as I have to rely on someone who is not technically trained to upload the files set the correct permissions etc etc and nothing seems to be working. Therefore I assumed it would be easier to attempt javascript as I know that already works and is client side. However if you're saying this isn't the best option I may have to explore others... Thanks Commented Apr 4, 2014 at 17:46

1 Answer 1

1

You'll want to use the JS navigator.userAgent method

For reference: http://www.w3schools.com/jsref/prop_nav_useragent.asp

For example:

<script>

var css;

switch(navigator.userAgent) {
    case 'BB10' // Or whatever the actual UA string is
        css = 'bb7.css';
        break;
    case 'iPhone' // Or whatever the actual UA string is
        css = 'iphone.css';
        break;
    default
        css = 'style.css';
        break;
}

var cssLink = document.createElement('link');
cssLink.setAttribute('rel', 'stylesheet');
cssLink.setAttribute('href', css);
document.getElementsByTagName('head')[0].appendChild(cssLink);

</script>

You could also use jQuery to append the stylesheet if it is available to you.

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

1 Comment

I will add that I agree with the other commenters that using JS for CSS switching based on UA strings is not ideal. I understand being limited by access and resources — which make solutions like this sometimes necessary — but I urge you to consider other strategies if this is not the only possibly way you're able to accomplish it.

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.