How to set javascript file for diff browsers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bgalan
    New Member
    • Feb 2008
    • 1

    How to set javascript file for diff browsers

    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]
    Last edited by acoder; Feb 20 '08, 07:51 AM.
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    You could load a small script from a src in the head of the document,
    whose only job is to load the correct script:

    [CODE=javascript](function(){
    var url;
    if(document.add EventListener)u rl= 'fs.js'
    else if(document.att achEvent) url= 'ie.js';
    if(url){
    var el= document.create Element('script ');
    el.type= 'text/javascript';
    el.src= url;
    document.getEle mentsByTagName( 'head')[0].appendChild(el );
    }
    })()[/CODE]

    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.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      [CODE=javascript]
      <!--[if IE]><script src="ie.js"></script> <![endif]-->

      <![if !IE]><script src="ff.js"></script> <![endif]>
      [/CODE]

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by rnd me
        [CODE=javascript]
        <!--[if IE]><script src="ie.js"></script> <![endif]-->

        <![if !IE]><script src="ff.js"></script> <![endif]>
        [/CODE]
        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?

        Comment

        Working...