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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    conditional javascript load based on browser

    Hi-

    I am trying to do a detection script - if the user has IE then one .js file loads if it is any other browser, than another .js file loads.

    With a little help this is what i have so far and it is not working:

    <script type="text/javascript" id="cScript"></script>

    <script type="text/javascript">
    function loadScript()
    {
    var conditionalScript = document.getElementById("cScript");

    if(getBrowser() == "IE")
    conditionalScript.src = "custom.js";
    else if(getBrowser() == "otherbrowser")
    conditionalScript.src = "customx.js";
    }
    </script>


    help...
    Shayna

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <script type="text/javascript">
     <!--// 
      onload = function(){ // written by: WillyDuitt@hotmail.com \\;
       var script = document.createElement('script');
           script.type = 'text/javascript';
    
        if(document.uniqueID) script.src = 'custom.js';
        else script.src = 'customx.js';
        document.getElementsByTagName('head')[0].appendChild(script);
      }
     //-->
    </script>
    .....Willy

  • #3
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wow - i am a little lost with that one. i am not great with js yet, but trying hard to learn. Sadly, neither .js file loads when i place that script in my <head></head>.

    thanks though...

    S

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, you are doing something wrong if that script is not working for you... Check that the paths to your external files are correct and/or if you are using any other onload events... If you are still having problems, post a link or the complete codes, including all external files, zipped together as an attachment...

    As for understanding what is going on... It's pretty straightforward... The script uses the DOM to create a script element and then uses a method which is proprietarly unique to IE only [ if(document.uniqueID) ] to check for IE without allowing browsers such as opera which support document.all and/or other browsers or firewalls which are spoofing their userAgents, and once the source is set, append the script to the documents head...

    .....Willy


  •  

    Posting Permissions

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