0

I am trying to simply hide/show an element depending if you're on mac, windows, iphone, ipad etc.

I have 2 p elements: p1 - I want to show on windows p2 - I want to show on everything else (mac/iphone/ipad etc)

I found a script here that apparently detects operating system: http://www.stoimen.com/blog/2009/07/16/jquery-browser-and-os-detection-plugin/

So I linked to the script after a link to jquery, plus this to the page head:

if ($.client.os == 'Mac') {
    $('#download').hide(); $('#register').show();
}

if ($.client.os == 'iPhone') {
    $('#download').hide(); $('#register').show();
}

if ($.client.os == 'Windows') {
    $('#download').show(); $('#register').hide();
}

I cannot get it to work, any ideas?

6
  • What is the value of $.client.os? Any errors? You may want to look at user agent as opposed to operating system. Commented Apr 1, 2013 at 19:38
  • you should add the class of the OS to body tag and hide your stuff in the CSS based on body class Commented Apr 1, 2013 at 19:38
  • Are you include jquery.client.js Commented Apr 1, 2013 at 19:41
  • @TamilSelvan yes I was including it. Commented Apr 1, 2013 at 19:51
  • @Huangism do you have an example of this? Commented Apr 1, 2013 at 19:51

1 Answer 1

0

This thread has some solutions to your problem. Given the properties you're specifying, all you should need to do is:

if (navigator.appVersion.indexOf("Win")!=-1) {
    $('#register').hide();
    $('#download').show();
}

You shouldn't need to explicitly show elements since the client won't be changing OS. Just set the base visibility of #download and #register to whatever you 'default' configuration is (put #download { display: none; } in your CSS) and let the .hide() and .show() override them when necessary.

If you need more advanced OS detection, you can add other rules by replacing "Win" with an appropriate OS tag.

EDIT fixed jsFiddle: http://jsfiddle.net/2G4pz/5/

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

9 Comments

Thanks metadept but this doesn't make sense if I have 2 elements I would have to hide element 1 and show element 2 if on mac/Ios and then if on windows hide element 2 and show element 1. Am I making sense here?
@James Yes, I'm just saying that you don't need to change the visibility for every case, you can have things as you want them for Mac by default, and then you only need to change them if the user is on Windows. If you want to explicitly set them that's fine, but it's not needed.
Thanks I get you, so you're saying as long as I have #download {display:none;} in my css the script above will over-ride this on windows machines and hide #register and show #download ?
@James Exactly. Here's a (very very simple) jsFiddle that demonstrates what I'm talking about: jsfiddle.net/2G4pz/3
Thanks @metadept I used your above code and tested on a windows machine though and still get the register div and not the download?
|

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.