0

I've been working on this for over an hour now and don't know whats wrong with it... code is below

var webkit = (browsername == (("Safari")||("Chrome")))? true : false;

It should return a true value for chrome and safari but not Internet Explore, Opera or Firefox I don't get why it returns true with safari but not chrome? any suggestions?

Even when I change it up a bit it still does this

if (browsername == (("Safari")||("Chrome")))
        webkit = true;

Am I doing my or wrong?

ryanc1256
  • 1,025
  • 1
  • 8
  • 14
  • `"safari||"Chrome" ==> "Safari"`, since `"Safari"` is truthy – John Dvorak Mar 26 '13 at 07:37
  • try this http://www.javascripter.net/faq/browsern.htm – btevfik Mar 26 '13 at 07:37
  • 1
    Actually, why do you need to browser-detect? – John Dvorak Mar 26 '13 at 07:38
  • 2
    PS. It's recommended to use the `===` operator, at least when you don't know the difference. See http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – Sami N Mar 26 '13 at 07:40
  • Its just for something that I am working on, I don't really need it, but may need it in the future :D – ryanc1256 Mar 26 '13 at 07:40
  • @ryanc1256 except for extreme cases, if you need to know what the browser is you're doing it wrong because tests based on specific browser versions are brittle. For this reason jQuery has deprecated the `$.browser` feature. Look at using something like `Modernizr` to test for _feature_ compatibility, not _browser_ versions. – Alnitak Mar 26 '13 at 07:45

5 Answers5

3

Yes, you do. It should be

var webkit = (browsername == "Safari")||(browsername == "Chrome");
Axel
  • 13,939
  • 5
  • 50
  • 79
1

use this type

if (browsername == ("Safari")||browsername == ("Chrome"))
    webkit = true;
Nilesh patel
  • 1,216
  • 2
  • 14
  • 38
0

You can't use || that way, you should use:

var webkit = (browsername == "Safari" || browsername == "Chrome");

and

if (browsername == "Safari" || browsername == "Chrome") { ... }
ulentini
  • 2,413
  • 1
  • 14
  • 26
0

Yes, I think you're using or and the == operators the wrong way. == in JavaScript can only compare one value with one different value at once and therefore works different than its use in spoken language. Try:

var webkit = (browsername == "Safari" || browsername == "Chrome") ? true : false;

EDIT: Thanks to Alnitak for the hint about the redundant ternary operator:

var webkit = browsername == "Safari" || browsername == "Chrome";
Marcellus
  • 1,277
  • 8
  • 7
  • Actually I see now you were just copying the OP but I can't revert the downvote until the answer is edited. – Alnitak Mar 26 '13 at 07:47
  • downvote reverted. But please put braces around the entire expression, and use `===` instead of `==`. – Alnitak Mar 26 '13 at 16:15
0

you can also try this in jquery:

    $(document).ready(function () {
        var browser=$.browser.webkit;
    });

It will return true for chrome and safari and "undefined " for other browsers

Gurmeet
  • 3,094
  • 4
  • 19
  • 43