Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browser detection and platform function Mac Safari and Firefox

Status
Not open for further replies.

MeganJane2

Technical User
Jun 21, 2006
1
AU
I need help to get the function below to work in Mac Safari and Firefox. Thanks in Advance:)

//browser detection
function Detect_Browser_and_Platform(){
agent = navigator.userAgent;
code = navigator.appCodeName;appname = navigator.appName;version = navigator.appVersion;

// ----- ----- ----- ----- ----- Detection Code
OS="other";
if (agent.indexOf("Win")!=-1){OS = "win";} if (agent.indexOf("Mac")!=-1){OS = "mac";}
Ver="0";
if (agent.indexOf("la/2") != -1){Ver="2";}
if (agent.indexOf("la/3") != -1){Ver="3";}
if (agent.indexOf("la/4") != -1){Ver="4";}
if (agent.indexOf("la/5") != -1){Ver="5";}
Browser="NS";
if (agent.indexOf("MSIE") != -1){Browser="IE";}
if (!agent && !code && !appname && !version){ //set for ie3
OS="win";Ver="3";Browser="IE";
}
// ----- ----- ----- ----- ----- End of Detection Code

//alert("Using "+Browser+" version: "+Ver+" on "+OS);

//-------------------------------------------
//ALL MACINTOSH STUFF HERE
if ((OS=="mac")&&(Browser=="NS")&&(Ver==3)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}
if ((OS=="mac")&&(Browser=="NS")&&(Ver==4)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}

if ((OS=="mac")&&(Browser=="IE")&&(Ver==3)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}//If this is the case, your in trouble... IE3 (Mac) doesn't actually support Script
if ((OS=="mac")&&(Browser=="IE")&&(Ver==4)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}

//-------------------------------------------
//ALL WINDOWS STUFF HERE
if ((OS=="win")&&(Browser=="NS")&&(Ver==2)){return("<body bgcolor='#FFFFFF' background='./images/front/tile_ns2.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}
if ((OS=="win")&&(Browser=="NS")&&(Ver==3)){return("<body bgcolor='#FFFFFF' background='./images/front/tile_ns3.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}
if ((OS=="win")&&(Browser=="NS")&&(Ver==4)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}

if ((OS=="win")&&(Browser=="IE")&&(Ver==3)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}
if ((OS=="win")&&(Browser=="IE")&&(Ver==4)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}
if ((OS=="win")&&(Browser=="IE")&&(Ver==5)){return("<body bgcolor='#FFFFFF' background='./images/front/tile.gif' leftmargin='0' topmargin='0' marginheight='0' marginwidth='0'>");}

}


document.write(Detect_Browser_and_Platform())
//END OF SCRIPT
// -->
 
Do you have access to a Mac? If so, run up the programs, and type into the URL bar:

Code:
javascript:alert(navigator.userAgent); alert(navigator.appCodeName); alert(navigator.appName); alert(navigator.appVersion);

Then you can find unique strings in those alerts to plug in for the correct values.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I'm running the latest Safari on the latest MacOSX10.4 release... and here is what I got running Dan's suggestions:

alert(navigator.userAgent);
Code:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3
alert(navigator.appCodeName);
Code:
Mozilla
alert(navigator.appName);
Code:
Netscape
alert(navigator.appVersion);
Code:
5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3

Hope this helps,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Maybe you missed it in the original post, Dan:
...Thanks in Advance:)
Let me translate that for you:
... Rather than thank you in person for the effort you went to in answering my problem, and rather than taking a few moments to describe how I solved my problem (so future netizens can benefit as well), I'll just say thanks now... it'll save me the hassle of having to interact with other people, too! :)
Ah well... we could always continue the thread and provide complete solutions in their absence I guess.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top