Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2006, 01:20 AM   PM User | #1
TrainReq
Guest

 
Posts: n/a
Load JS file if certain browser

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.
  Reply With Quote
Old 10-15-2006, 03:52 AM   PM User | #2
VortexCortex
Regular Coder

 
Join Date: May 2005
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
VortexCortex is an unknown quantity at this point
Exclamation Don't detect browsers, detect functionality.

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:
Code:
<head>
<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. */
//-->
</script>

<!-- 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); 

//--></script>
</head>
Change: document.attachEvent and document.addEventListener to the functions required by the scripts they load.
VortexCortex is offline   Reply With Quote
Old 10-15-2006, 04:11 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 10-15-2006, 04:37 AM   PM User | #4
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
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?
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Kravvitz is offline   Reply With Quote
Old 10-15-2006, 04:47 AM   PM User | #5
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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){
if(document.implementation.hasFeature('html','2.0')// load eventListener code for firefox, opera and Safari
else if(document.attachEvent)// load IE event handling code
}

Last edited by mrhoo; 10-15-2006 at 04:55 AM.. Reason: clarification
mrhoo is offline   Reply With Quote
Old 10-15-2006, 05:37 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.

Code:
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;
}
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 10-15-2006 at 05:43 AM..
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:05 PM.


Advertisement
Log in to turn off these ads.