...

View Full Version : Calling a JS file based on browser detection



greasonwolfe
11-12-2004, 09:12 AM
I am not sure if this would be a valid IF statement or not so I wanted to pass it through you fine people here to see what your thoughts on it are. I know I can detect a browser and then go to a specific page based upon the browser type, but can I do the same and call up a JS file instead? My thoughts are something along the lines of the following. . .

if (document.all) {javascript src="dothis.js")
else if (document.getElementById) {javascript src="dothat.js"}

Kor
11-12-2004, 09:33 AM
Have you consider detecting the browser inside the code, seting the objects according to the detector and build this way a unique crossbrowser js file?

greasonwolfe
11-12-2004, 10:24 AM
Actually, no, I hadn't thought of that, but that is a right fine idea and thanks for passing it along. I will definately look into that aspect of the coding.

:thumbsup:

Kor
11-12-2004, 10:58 AM
Something like this:


<script language="JavaScript" type="text/JavaScript">
function createObj(id){
if(document.layers){
this.obj = document.layers[id];
this.obj.style.width = document.layers[id].width;
this.obj.style.height = document.layers[id].height;
}
else{
if(document.getElementById){
this.obj=document.getElementById(id);
}
else if(document.all){
this.obj=document.all[id];
}
this.obj.style.width = document.getElementById(id).style.width;
this.obj.style.height = document.getElementById(id).style.height;
}
return this.obj;
}</script>


Now whenever you need to change dinamically some style attributes' values just call that function



oFoo = new createObj('foo_id');
oBla = new createObj('bla_id');
oFoo.style.width = 30+'px';
oBla.style.height = 50+'px'

greasonwolfe
11-12-2004, 11:30 AM
Right, I getcha now and once more, thanks bunches. I am trying to make my pages as crossbrowser compatible as possible and think this will help very much. It has been quite some time since I have tried to do this sort of coding other than html. .. most of my previous experience being with (shudder) apple basic, 6502 machine language and early versions of pascal, but I am slowly getting the hang of it again thanks to all the help you guys here are providing.

:thumbsup:

Kor
11-12-2004, 12:08 PM
there wes a mistake in my previous. the correct is



<script language="JavaScript" type="text/JavaScript">
function createObj(id){
if(document.layers){
this.obj = document.layers[id];
this.obj.style.width = document.layers[id].width;
this.obj.style.height = document.layers[id].height;
}
else if(document.getElementById){
this.obj=document.getElementById(id);
this.obj.style.width = document.getElementById(id).style.width;
this.obj.style.height = document.getElementById(id).style.height;
}
else if(document.all){
this.obj=document.all[id];
this.obj.style.width = document.all[id].style.width;
this.obj.style.height = document.all[id].style.height;
}
return this.obj;
}
</script>


Anyway, I think you got the point...

In fact... hm... There are very very few users with IE4(document.all) and NS4(document.layers). Maybe there is no need to crossbrowser as all the modern browsers use now document.getElementById reference...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum