View Full Version : Load JS file if certain browser

10-15-2006, 02:20 AM
Basicly. I am making a .js file.. and i basicly want something in the JS file to where it basicly says... load js file if IE or load js file if firefox. I do not want to use <if IE HTML commands on the outside HTML page.. but i would like to use something like <if IE or if firefox within the js file...

baiscly.. is there any line of javascript that says , only preform this javascript if it is in a firefox or IE browser.

10-15-2006, 04:52 AM
Ok, I know that it is standard practice to "sniff" out a browser, but this isn't what you really wanted to do! What you really want to know is whether or not the user's current browser is compatible with your script. It could be any browser as long as it supports the functionality your script requires.

So, the question shouldn't be: Is this IE or is this NS/FF?
Ask instead: Does this browser have document.attachEvent function or a document.addEventListener function?
(if you are using those functions, that is)

If you test for functionality you can avoid the trap of sniffing the user agent string. Besides, FF has a plugin that allows you to change the UA to match IE which fools those kind of scripts and breaks the page.

In the head section of your page:

<script type="text/javascript"><!--
var scriptStr = 'unknown.js';

if (typeof document.attachEvent != 'undefined') scriptStr = 'ie_compatible.js';

if (typeof document.addEventListener != 'undefined') scriptStr = 'ns_compatible.js';

document.write('<scr'+'ipt type="text/javascript" src="'+scriptStr+'"></script>');
/* Do not put any code here that relys on
either of the three scripts mentioned above. */

<!-- One of the above scripts will be loaded here -->

<script type="text/javascript"><!--
/* Now you can use code that depends on
any of the three scripts mentioned previously. */

//defined in one of the scripts (unkown.js, ie_compatible.js or ns_compatible.js)
BrowserIndependent.attachListener(myElement, myFunct, true);


Change: document.attachEvent and document.addEventListener to the functions required by the scripts they load.

10-15-2006, 05:11 AM
If you are going to sniff out IE and NN then shouldn't you also sniff out which of the 10,000 other browsers that exist as well? Of course just about all of them report themselves as IE anyway to get around browser sniffers anyway so it is just about impossible to identify what browser it actually is as most will lie.

10-15-2006, 05:37 AM
TrainReq, I take it you are having trouble writing scripts that work in IE and Firefox. Why don't you ask us how to rewrite them so they are cross-browser compatible instead of making multiple versions of the same script?

10-15-2006, 05:47 AM
I write most of my code to work in any browser, but the event models of IE and everybody else require either running an 'if(window.event)' sniffer on every event or separate scripts. I prefer to check the user environment once, as I load the page, instead of in every event.

I load scripts incrementally, as needed, and the special purpose scripts are each about 2kb.

if(document.implementation.hasFeature('html','2.0')// load eventListener code for firefox, opera and Safari
else if(document.attachEvent)// load IE event handling code

10-15-2006, 06:37 AM
You should test if the browser supports event listeners in order to use event listeners. There may be a browser out there where one of document.implementation.hasFeature('html','2.0') and event listeners are supported by the browser. Here's a cross browser function that will add whichever the browser supports.

function addEvent(el, eType, fn, uC) {
if (el.addEventListener) {
el.addEventListener(eType, fn, uC);
return true;
} else if (el.attachEvent) {
return el.attachEvent('on' + eType, fn);
} else {
el['on' + eType] = fn;