469,326 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

How to set javascript file for diff browsers

1
Hi,

I have 2 javascript file : IE.js and FF.js
If the browser is IE i want to use the IE.js file.
or if the browser is Firefox ill use the FF.js
the functions on IE and FF are the same but different in some codes not supported by either browser.

how can i do this in my html page?

for example

IF browser = IE
<script src="IE.js" type="text/javascript" />
ELSE
<script src="FF.js" type="text/javascript"/>

Thanks,[email removed]
Feb 20 '08 #1
3 1060
mrhoo
428 256MB
You could load a small script from a src in the head of the document,
whose only job is to load the correct script:

Expand|Select|Wrap|Line Numbers
  1. (function(){
  2.     var url;
  3.     if(document.addEventListener)url= 'fs.js'
  4.     else if(document.attachEvent) url= 'ie.js';
  5.     if(url){
  6.         var el= document.createElement('script');
  7.         el.type= 'text/javascript';
  8.         el.src= url;
  9.         document.getElementsByTagName('head')[0].appendChild(el);
  10.     }
  11. })()
This syntax in a remote script called from the head will add the new script before the body is displayed.
You could easily call it onload instead, if you want the page up as quickly as possible.
Feb 20 '08 #2
rnd me
427 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. <!--[if IE]><script src="ie.js"></script>    <![endif]-->
  2.  
  3. <![if !IE]><script src="ff.js"></script>    <![endif]>
  4.  
Feb 21 '08 #3
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. <!--[if IE]><script src="ie.js"></script>    <![endif]-->
  2.  
  3. <![if !IE]><script src="ff.js"></script>    <![endif]>
  4.  
Yep, conditional comments.

To the OP: you should consider if you could combine the two, though sometimes it's not possible. Have you thought about other browsers too?
Feb 21 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Charley | last post: by
16 posts views Thread by Christopher Benson-Manica | last post: by
8 posts views Thread by chrisdude911 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.