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
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Calling a JS file based on browser detection

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

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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.


  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Something like this:
    PHP Code:
    <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

    PHP Code:
    oFoo = new createObj('foo_id');
    oBla = new createObj('bla_id');
    oFoo.style.width 30+'px';
    oBla.style.height 50+'px' 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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.


  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    there wes a mistake in my previous. the correct is

    PHP Code:
    <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...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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