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
BrowserDetectobject you use.Try
and call it with