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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detect browser, each to use different stylesheet 1

Status
Not open for further replies.

duph

Programmer
Sep 22, 2005
20
US
I need a script that will detect the user's browser and use the appropriate stylesheet that determines position of my form. (I am using a background image for my form, and the inputs need to line up correctly.) All the browsers are rendering pos differently.

Can anyone lend a hand?

Tried to use the code at thread216-936436 that dwarfthrower suggested:

Code:
if (browser != "Internet Explorer")
{
document.write('<link href="ie.css" rel="stylesheet" type="text/css">');
}
else
{
document.write('<link href="firefox.css" rel="stylesheet" type="text/css">');
}

but clearly I'm missing some pieces.

Thanks!
 
You can perform this with a few tricks. Since some javascript commands are supported in certain browsers you can use this fact to determine which is being used. In this case we can use document.all (as it is IE only):
Code:
if (document.all) {
   document.write('<link href="ie.css" rel="stylesheet" type="text/css">');
}
else {
   document.write('<link href="firefox.css" rel="stylesheet" type="text/css">');
}

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Wow, this works perfectly. Thank you!

If I want to add stylesheets for more browsers, e.g. safari, do I add another document.write statement after the semicolon in "else" with my safari.css file ref'd ?
 
I've found this doesn't work for Opera, as document.all = true in Opera.

so document.all is not a way to detect if user has I.E.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
duph,

Another option would be to try and fix your CSS so that you don't need to use different code for each browser.

Personally, I'd say that is the better route to go down, before falling back on a JavaScript solution.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Another option would be to use conditional comments... providing a solution that works without javascript!


Code:
<link href="default.css" rel="stylesheet" type="text/css">
<!--[if IE]>
<link href="ie.css" rel="stylesheet" type="text/css">
<![endif]-->

You would put your IE specific CSS in the second file and it will only be included on the page for IE.

This will obviously require you to change the approach (of one CSS per browser)... but since that particular idea is not suitable for modern browsers... I don't see a problem.

Cheers,
Jeff

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

What is Javascript? FAQ216-6094
 
I can't see an ELSE that goes with this comment, is there one ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
If there is, it's not officially documented. And to be honest, that wouldn't surprise me. IE CCs can be used for all sorts of things that Microsoft haven't openly (AFAIK) documented (such as MS Office detection).

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top