Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    TrainReq
    Guest

    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.

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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;
    }
    }
    Last edited by felgall; 10-15-2006 at 05:43 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •