0

I have this code:

<body onload="detect(navigator.appName)">
<h2 id=alert>This tutorial is for Google Chrome users,
why would you want to read it?</h2>
</body>
<script>
function detect(x){
    alert(x)
    if (x != "Chrome"){
        document.getElementById("alert").style.display = '';
    }
}
</script>

The problem is, whenever I open the page using Google Chrome, It returns "Netscape". Is there a workaround?

Webpage

SeinopSys
  • 8,787
  • 10
  • 62
  • 110
  • 1
    Because I'm on my phone that doesn't have chrome – Esailija Aug 10 '12 at 17:30
  • Good way to cut your audience down. – j08691 Aug 10 '12 at 17:31
  • What else have you tried? Also, what were you expecting to be returned? – WhyNotHugo Aug 10 '12 at 17:31
  • 1
    @j08691 It says in his code that it's a tutorial for Google Chrome users. Why bash him for asking a question that targets a specific browser? – Samuel Aug 10 '12 at 17:32
  • @Samuel - yes I realize that, however as Esailija noted, what if I have Chrome but might not have access to it at the moment? – j08691 Aug 10 '12 at 18:10
  • @j08691 See [Webpage](http://dl.dropbox.com/u/38516275/MyWebPage/FPP/Chrome_and_flash.html). It will not remove any info from the page, only it will display the message, nothing else. – SeinopSys Aug 10 '12 at 18:16

2 Answers2

3

On chrome this is what navigator.appName will give you:

Netscape

It will be much easier if you parse the User Agent

if ( ! /(Chrome)/i.test(navigator.userAgent)) {
    document.getElementById("alert").style.display = '';
}

But i have to say, if you show your tutorial to only those who are using Chrome, you are missing the point of the internet.

JDavis
  • 3,228
  • 2
  • 22
  • 23
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • 1
    `if (/(Chrome)/i.test(navigator.userAgent)) { document.getElementById("alert").style.display = ''; }` – JDavis Aug 10 '12 at 17:38
  • Hiding content with JS will still show the content until the script is loaded. I would suggest hiding it with CSS and then show it if the conditions aren't met. – JDavis Aug 10 '12 at 17:40
  • @JDavis There's actually a `style="display:none;"` attached to the `

    ` in the final code, I removed it here because it's not a neccesity.

    – SeinopSys Aug 10 '12 at 17:43
1

Try jQuery's browser function http://api.jquery.com/jQuery.browser/. It's tested and true.

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • I'm not into jQuery that much. – SeinopSys Aug 10 '12 at 17:32
  • This isn't a jQuery question. There's no jQuery tag. – jfriend00 Aug 10 '12 at 17:34
  • @jfriend00 Just because there is no tag doesn't mean it can't be suggested. – Samuel Aug 10 '12 at 17:37
  • 1
    The general protocol here on StackOverflow is that you only propose a solution using a library such as JQuery, Prototype, YUI, etc... if that is asked for in the question. Otherwise, plain javascript solutions should be offered or you should use a comment on the question to ask for clarification about the desired for answers using a library. – jfriend00 Aug 10 '12 at 17:45
  • What if OP had never heard of jQuery? I think he would be pretty happy to discover it. Or if somebody searches for this question they'll have the tip to use jQuery. The OP can decide which is the right answer. – Samuel Aug 10 '12 at 17:49
  • @Samuel I have heard of jQuery, I just don't feel like learning it if I don't necessarily need it. – SeinopSys Aug 10 '12 at 18:52