View Full Version : conditional javascript load based on browser

07-09-2005, 07:22 PM

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";


Willy Duitt
07-09-2005, 08:26 PM
Try this:

<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';


07-09-2005, 10:33 PM
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...


Willy Duitt
07-10-2005, 01:04 AM
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...