sarah3585
sarah3585

Reputation: 637

User agent detection to include js file

I'm trying to write some detection code. If user agent is iPad, then write this javascript but it's not working.

<script type="text/javascript">
  if(navigator.userAgent.match(/iPad/i)) {
    document.write('<script type="text/javascript"
       src="photoswipe/code.photoswipe-3.0.4.min.js"></script>');
  }
</script>

Any ideas?

Upvotes: 1

Views: 5629

Answers (1)

Sarfraz
Sarfraz

Reputation: 382746

Try this (from David Walsh's blog: http://davidwalsh.name/detect-ipad) :

// For use within normal web clients 
var isiPad = navigator.userAgent.match(/iPad/i) != null;

// For use within iPad developer UIWebView
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);

Using it:

if(isiPad){
  document.write('<script type="text/javascript" src="photoswipe/code.photoswipe-3.0.4.min.js"><\/script>');
}

Upvotes: 2

Related Questions