Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a CSS stylesheet only for iOS devices

Tags:

jquery

css

ipad

How can I add a stylesheet dedicated only for iOS mobile devices?

I want to run a website which contains flash. Obviously flash elements won't work on iPhone, iPod and iPad. Due to this fact I would like to create a CSS file ONLY for these devices. I am mostly worried about iPad, as i can use @media screen size properties for other two.

What I have so far is this:

var IS_IPAD = navigator.userAgent.match(/iPad/i)  != null;

if(IS_IPAD)    ({
      url: href,
      dataType: 'css',
      success: function(){                  
            $('<link rel="stylesheet" type="text/css" href="'+css/ipad.css+'" />').appendTo("head");
        }
});

What is not working here?

like image 740
Piotr Ciszewski Avatar asked Nov 12 '13 14:11

Piotr Ciszewski


Video Answer


2 Answers

you could do something like this:

var is_ios = /(iPad|iPhone|iPod)/g.test( navigator.userAgent );

which would return true or false.

then:

if(is_ios)    
{
 $('<link rel="stylesheet" type="text/css" href="css/ipad.css" />').appendTo("head");
};

UPDATE:

A different approach, instead of using userAgent detection, would be to test for browser support. You can do it with jQuery like this:

$('html').addClass(typeof swfobject !== 'undefined' && swfobject.getFlashPlayerVersion().major !== 0 ? 'flash' : 'no-flash');

This works in a "Modernizr" style way of adding a class to the html tag of the document, so in your css you can do this:

 html.flash .element {
 /*Do something here if flash if enabled*/
 }

 html.no-flash .element {
 /*Do something different here if flash if not available*/
 }
like image 170
wf4 Avatar answered Sep 18 '22 00:09

wf4


Why not just this:

var is_ipad = navigator.userAgent.match(/iPad/i) ? true : false;

if(is_ipad)
  $('<link rel="stylesheet" type="text/css" href="path/to/css.css" />').appendTo('head');
like image 30
Bn Mk Avatar answered Sep 18 '22 00:09

Bn Mk