0

I've been looking for an answer to this question for 4 hours now, with no success.

Well, what I'm trying to do execute a function that does something if the user is using a certain browser.

I have the script used for detecting the browser, version, and OS in the head section of the page, and it works.

If I use the code below in the body of my page it works fine.

<script type="text/javascript">
if (BrowserDetect.browser == "Chrome")
{
 document.write("You're using Chrome")
}
else
{
 document.write("You're not using Chrome")
}
</script>

But if I put the code in an external script sheet, how do I use it? I tried putting it in a function and calling that function on load by using this code.

<body onload="BrowserDetect();">

Note that the external script sheet is called in the head section of my page.

And this is the code in the external script sheet.

function BrowserDetect()
{
 if (BrowserDetect.browser == "Chrome")
 {
 document.write("You're using Chrome")
 }
 else
 {
 document.write("You're not using Chrome")
 }
}

As you can see, it's the exact same code that worked when it was in the body of the web page. But when it's put in a function and called on load, it doesn't work. Why?

asked Sep 19, 2011 at 8:26
2
  • Browser sniffing is usually a bad idea (unless it's used for a "hey, use a better browser"-style message) Commented Sep 19, 2011 at 8:33
  • Does any error occurs? What output into javascript console? Commented Sep 19, 2011 at 8:37

3 Answers 3

3

Because you have named the called method the same as the BrowserDetect object you use.

Try

function BrowserDetectMethod()
{
 if (BrowserDetect.browser == "Chrome")
 {
 document.write("You're using Chrome")
 }
 else
 {
 document.write("You're not using Chrome")
 }
}

and call it with

<body onload="BrowserDetectMethod();">
answered Sep 19, 2011 at 8:30
Sign up to request clarification or add additional context in comments.

Comments

1

Try changing the name of the function to something other than BrowserDetect.

answered Sep 19, 2011 at 8:29

Comments

0

The real problem I was having from the beginning was that my scripts worked if I had them on the page, but I wanted them in an external style sheet, and if I had them in an external script sheet, the scripts didn't work. So I found out how to fix it, and I'll list below everything I did to make everything listed above work perfectly.

For those of you wondering how I detected the browser, I got it from this site: http://www.quirksmode.org/js/detect.html. That works sooooooooo much better and easier detecting it using normal javascript.

First of all, the function should be named something other than BrowserDetect; I named it BrowserDetectMethod, like suggested by Gaby.

And second, if I am going to include the function in an external style sheet, the external style sheet needs to be called at the bottom of the page. Not in the head of the page like I was trying.

Third:

I decided the best way to browser sniff was to find the browsers I wanted to contact, not the ones I didn't. (since there are any number of browsers out there and I only wanted to contact three)

However, using the above method, I couldn't do that because this code doesn't work:

function BrowserDetectMethod()
{
 if (BrowserDetect.browser != "Chrome" && BrowserDetect.browser && "Safari" ||BrowserDetect.browser && "Firefox" )
 {
 document.write("You're not using a supported browser");
 }
 else
 {
 document.write("You're using a supported browser");
 }
}

The reason that code doesn't work is because it can't contain more than one "and" logical operator in the if statement.

So I had to achieve the desired affect by nesting if statements like so:

function BrowserDetectMethod()
{
 if (BrowserDetect.browser != "Chrome")
 {
 if(BrowserDetect.browser != "Firefox")
 {
 if(BrowserDetect.browser != "Safari")
 {
 document.write("You're not using a supported Browser");
 }
 else
 {
 document.write("You're using a supported Browser");
 }
 }
 else
 {
 document.write("You are using a supported Browser");
 }
 }
 else
 {
 document.write("You are using a supported Browser");
 }
}
answered Sep 21, 2011 at 8:27

1 Comment

you can use multiple logical operators. You just have not written it correctly.. So use var b = BrowserDetect.browser; if( b != 'Safari' && b != 'Firefox' && b != 'Chrome') {// supported} else {//not supported}

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.