Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Best way to detect Safari browser using jQuery

Posted on 2014-02-17
3
8,124 Views
Last Modified: 2014-02-17
I would like to know the best way to detect if a browser is specifically Safari using jQuery.

I am aware that webkit is shared by Chrome and Safari but I am specifically interested to know if the browser is Safari and nothing else.
0
Comment
Question by:mike99c
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39864926
var isSafari = navigator.vendor.indexOf("Apple")==0 && /\sSafari\//.test(navigator.userAgent); // true or false

Open in new window

0
 
LVL 13

Assisted Solution

by:duncanb7
duncanb7 earned 250 total points
ID: 39865077
Dear mike99c,

You can use the following javascript code to test Safari browser or not
I put the  document.write in if statement for Safari browser detection only
and display it into body tag.
And if it is not Safari browser, it will keep the  message of "It is NOT Safari Browser" in body tag.

The document.write you can put in other if statement for other browser you want to test

Hope I understand your question completely, if not please pt it out,
The code is Not fully tested

Duncan


<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "/WEB/INF/xhtml1-strict.dtd">                                                  
  <html> <head> <meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8"/> 
  <meta http-equiv="CACHE-CONTROL" content="NO-STORE"/> 
  <meta http-equiv="EXPIRES" content="-1"/> 
  <title>Safari Browser Test</title> 
  <script type="text/javascript">
  function init(){
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
   
   txt ="<p id='pd0' style='color:blue'> It is Safarai Browser, Browser name  ="+browserName+'</p>';
   document.write(txt);
}
}
</script>
</head>
<body onload="init();">
It is NOT Safari Browser
</body>
</html>

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39865159
Thanks for your pt, there is a lot of  solution in google search
for Jquery browser detection but that might NOT  be
consistent and that some  is depended on the jquery plugin version.

You can simplify those javascript code for  your need that is not depend on
javascript version

Have a nice day

Duncan
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question