|
|||||||
| CSS Discussion and technical support relating to Cascading Style Sheets. |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Auto-detect Browsers?
i think i read somewhere that i can use javascript to determine what browser the user is using and have the script assign an appropriate stylesheet. is this possible? can anyone tell me how to do this or where to find info on this subject? sorry if this is the wrong forum for this but i was torn about who might know more about this.
|
|
#2
|
||||
|
||||
|
Auto-detect Browsers?
PHP Code:
Last edited by griff777; 06-18-2005 at 06:10 PM. |
|
#3
|
||||
|
||||
|
And the headerscript.htm file...
HTML Code:
<!-- use PHP or SSI to include this in the header of an html file --> <!-- Begin headerScript.htm --> <!-- NOTE: DO NOT INSERT type="text/javascript" in the following detection scripts... It WILL cause them to incorrectly detect a version that is NOT valid. --> <script language="JavaScript" > jsv = 1.0; </script> <script language="JavaScript1.1" > jsv = 1.1; </script> <script language="JavaScript1.2" > jsv = 1.2; </script> <script language="JavaScript1.3" > jsv = 1.3; </script> <script language="JavaScript1.4" > jsv = 1.4; </script> <script language="JavaScript1.5" > jsv = 1.5; </script> <!-- the following versions do not exist as yet... 6/11/2005 I have put this "check" in to verify that the short detection scripts actually work... --> <script language="JavaScript1.6" > jsv = 1.6; </script> <script language="JavaScript1.7" > jsv = 1.7; </script> <script language="JavaScript1.8" > jsv = 1.8; </script> <script language="JavaScript1.9" > jsv = 1.9; </script> <script language="JavaScript2.0" > jsv = 2.0; </script> <!-- the above script detections and "navCheck.js" work hand in hand... --> <script src="navCheck.js" type="text/javascript"> /* load the nav check javascript, a continuation of our script here... */ </script> <script type="text/javascript"> /* OK, our main javascript should be working, let's setup our CSS lines */ css1 = "electrom.css"; css2 = "electrom_ns.css"; css = (IE4) ? css1 : css2; document.writeln('<link rel="STYLESHEET" type="text/css" href='+css+'>'); badJS = "js_iv.htm"; if(jsv < minJSver) window.location = badJS; // don't allow incorrect versions of javascript... </script> <script src="clock.js" type="text/javascript"> </script> <!-- Notify the web surfer that their browser isn't using javascript--> <noscript> <pre> Javascript is NOT active.<br> Please note, to view this site JAVASCRIPT must be active.<br> </pre> </noscript> <!-- End headerScript.htm --> Last edited by griff777; 06-18-2005 at 06:08 PM. |
|
#4
|
|||
|
|||
|
Or another script, which is a lot smaller. You just paste this in the header of each html page. You could also separate the actual javascript code into an external page and call it in the header of each html page (in which case you would leave the <noscript>alsjdfajdsflkjdf</noscript> part in the header of each page too).
in the even that a user doesn't have javascript enabled, the <noscript>jaldjfdfj</noscript> loads a default stylesheet. you'd probably want to make this the correct stylesheet for IE since most users will be on IE. <script type="text/javascript"> <!--// Stylesheet Switcher if ((navigator.appName).indexOf("Microsoft")!=-1) { document.write('<link rel="stylesheet" href="styleIE.css" type="text/css">'); } else { document.write('<link rel="stylesheet" href="style.css" type="text/css">'); } //--> </script> <noscript> <link rel="stylesheet" href="style.css" type="text/css"> </noscript> |
|
#5
|
|||
|
|||
|
are there any significant differences in the different scripts listed? i would assume that i would want to use the shortest one. also, if i write an external script, what is the proper way to call it in the header. i would prefer to use that technique since there will be multiple pages on the site where this test will be necessary.
Last edited by jerseydubs; 06-18-2005 at 03:18 PM. |
|
#6
|
|||
|
|||
|
the proper way to do my script externally would be to put this in the header:
<script type="javascript" href="script.js"> <noscript><link rel="stylesheet" type="text/css" href="stylesheet.css"></noscript> then you just copy all my code up to the </script> part and stick it in another text file and save it as script.js the longer code is doing a lot of things: 1. checking to see the user's browser has javascript enabled. if not, it tells the user to enable javascript in order to see the site 2. checks for many different browsers - IE, mozilla, netscape, opera, webtv, safari, IE/Mac 3. checks for the particular version of that browser 4. checks the version of javascript suffice it to say, it's a very very thorough script that checks for every possible combination of javascript and browser version and thus allows you to create a website perfectly tailored to just about anybody. the bad part about this is the site can just get huge - you could have like 10 possible stylesheets for example, would you want to develop that much? mine is a little mroe basic. it checks the browser to see if it is Microsoft and then loads a stylesheet for Internet Explorer. if the browser is anything other than Microsoft it loads a different stylesheet. it cannot differentiate between opera, firefox, netscape, etc. so if you have separate stylesheets for each browser then this isn't a good script for you |
|
#7
|
||||
|
||||
|
Why are you messing with Javascript to work out which stylesheet to attach. There are quirks in the way that stylesheets themselves are coded that will allow you to attach different stylesheets for different browsers even with Javascript disabled eg specifying media="all" will stop Netscape 4 from using a stylesheet, you can use if statements in the HTML to select IE versions to attach stylesheets, etc.
__________________
Stephen |
|
#8
|
|||
|
|||
|
I use FireFox as do many other users and can therefore turn off javascripts and mine has a user agent switcher so it can pretend to be IE, Opera, etc so you really need to take Felgall's approach and save yourself a lot of work.
|
|
#9
|
|||
|
|||
|
Quote:
i was unaware of any other means. can you be more specific about this please? or point me to a good reference? my goal is to have a website that is 100% css and as compatible as possible. (if that's possible) |
|
#10
|
|||
|
|||
|
Search the boards for threads which might answer your question. That's a link by the way, to a thread in the javascript forum (you were asking about javascript, right?), which asks the same exact question as you.
Hope this helps!
__________________
-Anthony |
|
#11
|
|||
|
|||
|
Auto detect brosers to give a warning
Hi
I am also trying to put up a page which says that the page will work properly with Mozilla Fireox version xyz How to auto detect what browser the surfer is using and in case it is not Mozilla, how do we give hima warning? PLEASE HELP TIJU |
|
#12
|
||||
|
||||
|
Get it working in Firefox, Opera, Safari and Chrome first. That will get you writing standard CSS. Then come back here and we can help you hack the styles to get IE to play nice. It's a lot less work.
__________________
My Blog: FundaMental Disaster Accessible DHTML Tabs | Quick CSS Positioning Explanation | Quick Floated Elements Explanation | 50% + 50% != 100% | Gaps Under Images | ID vs. Class | Gappy Lists in Internet Explorer | Why Tables Are Slower | Benefits of XHTML vs. HTML | Linking to External Style Sheets | About DOCTYPES | Web Design is a Peanut Butter & Jelly Sandwhich | CSS: To Hack Or Not To Hack | Internet Explorer and Transparent PNGs |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|