How to link to diference js file base on specific target browser

Hi,
I have stupid question.
I’m trying to pull a specific js file which matches specific target browser like something below:


if browser = webkit
     <script src='webkit.js'></script>
else if browser = opera
     <script src='opera.js'></script>

Can I achieve this. How so?

Thanks in advance

On the side note: I don’t really want to target different browsers, just the feature associate with them.

So test for the features you need to distinguish on. JavaScript has no way to tell which browser is which apart from which features it supports.

Thanks for reply,

base on my research, document.write(“<script src=‘opera.js’></script>”); should work.

But as felgall said, you don’t know when to use that because you don’t know which browser someone is using. In JS, you can test that a feature is supported before actually using it … which is what felgall was saying.

Apart from the fact that you will never know when to call that because you can’t tell whether the browser is Opera or not unless your visitor tells you it is (in which case it might still be Internet Explorer or Firefox pretending to be Opera) the </script> there will terminate the script - you would need to specify it as <\/script> for it to work.

Also document.write became obsolete when Netscape 4 died. There are several better ways to generate HTML from JavaScript now.

I think this going to be giving me a headache.

What’s your aim with this, anyhow? It doesn’t sound very practical to target browsers in general.

I’m building a HTML5 mobile app. Unfortunately, there are only two browsers, namely chrome canary and opera labs, that support this sophisticated feature. A javascript app that work for chrome does not run on opera and vice versa and when I put them together none works. So I want to pull them separately base on the browser. Here’s the catch:


<script>
 if(browser = chrome) {
    document.write("<script src='webkit.js'></script>")
} else if(browser = opera) {
    document.write("<script src='opera.js'></script>")
}
</script>


JavaScript has no way to tell which browser is which apart from which features it supports. 

This is not true. I’ve a javascript snippet on another computer that tell me which browser the a user use when they visit my website, including its version. It’s just few line and very easy script. So I can’t show you that script right now as googling cannot render anything that quite close to the script which I use.

It is true because all you are collecting there is what your visitor has their browser set to identify itself as.

Opera, Chrome, and Safari can all identify themselves as Firefox or Internet Explorer.
Internet explorer and Firefox can identify themselves as anything at all.

If the browser claims to be “Eat at Joe’s” version 21 how are you going to tell if it is really IE 7 or Firefox 9 or some other version of one of those browsers?

If the browser identifies itself as Internet Explorer 27 how are you going to tell what browser iand version it really is?

All you can collect with the code you are referring to is what browser I decide to tell you I am using - and if you use that antiquated code (obsolete even in Netscape 2) then likely your site will not work propelry for many visitors and they will send you a fake value just to bypass your code. I have to do that to access the web site for my bank because they stupidly check for certain valuees in those fields even though their site works in other modern browsers as well.

Fortunately most modern browsers can be set to provide different information to different sites based on what browser you want the site to think you are using.

Thanks to make it clear. I’ll keep that in mind.

This is stupid considering this post: http://www.sitepoint.com/ie7-tax/