开发者

How to call a function

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?


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();">


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


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");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜