/    Sign up×
Community /Pin to ProfileBookmark

browser detect / document.write

I’m pretty much a javascript novice, and hope someone can help me with this attempt to write one link to a CSS file if the user is on Netscape 4, 4.7x etc. (where stuff tends to explode), and one for IE 5 and above. I’m trying to keep it as simple as possible. Here’s what I have so far:

<SCRIPT LANGUAGE=”JavaScript”>
<!–
if ((navigator.appName == “Netscape”) && (parseInt(navigator.appVersion) < 4 )) {
document.write(“<LINK REL=stylesheet HREF=”../file2.css” TYPE=”text/css”>”); }
else {
document.write(“<LINK REL=stylesheet HREF=”../file.css” TYPE=”text/css”>”); }
// –>
</SCRIPT>

Doesn’t seem to be working, though. Netscape 4.7 appears to still be calling up file.css instead of file2.css. The script is successfully writing file.css for IE 5, though. Can anyone help? Much appreciated if so.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Feb 12.2003 — [i]Originally posted by tenoruke [/i]

[B]

if ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) < 4 )) {

document.write("<LINK REL=stylesheet HREF="../file2.css" TYPE="text/css">"); }

[/B]
[/QUOTE]


My guess is that you never fullfile the condition above because its looking for a version below 4,if you are always looking for a version 4 then it should say ==4

cheers

Khalid
Copy linkTweet thisAlerts:
@AdamBrillFeb 12.2003 — I think the if statement should look like this:

if(navigator.appName=="Netscape" && parseInt(navigator.appVersion)<5)

You had some of your parenthesis wrong and now it will run the first one for version 4 and lower. You should put a <noscript> tag in there, too, just in case they don't have javascript...
Copy linkTweet thisAlerts:
@tenorukeauthorFeb 12.2003 — Thanks!! Based on both your sets of feedback, I have something that works. It looks like this:

<!--

if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion)<6) {

document.write("<LINK REL=stylesheet HREF="../file.css" TYPE="text/css">"); }

else {

document.write("<LINK REL=stylesheet HREF="../file2.css" TYPE="text/css">"); }

// -->

Instead of sniffing the 'wrong' browser (Netscape), I figured why not sniff the 'right' browser (MSIE),

since file2.css is the simpler one that works best on all Netscape versions. The specs are for IE 5 and Netscape 4 on up, mainly on PC / Windows 98 and up. Does this make the same amount of sense to you?
Copy linkTweet thisAlerts:
@CharlesFeb 12.2003 — [font=georgia]There is, however, a much easier and non-JavaScript way to do this. (Keep in mind that an awful lot of MSIE users disable scripting.) Netscape 4 doesn't recognize the CSS [font=monospace]@import[/font] rule. See http://www.w3.org/TR/REC-CSS2/cascade.html#at-import.[/font]
Copy linkTweet thisAlerts:
@tenorukeauthorFeb 12.2003 — Charles -

If I had my way, I wouldn't be using CSS for any part of this design. I lean toward stability over download speed, BUT the client here is enamored of 'saving k' by doing nav and mouseovers asked me to use CSS for the navigational elements. Bleah.

Thanks for the link, though. I'll give it a whirl!

Tenoruke
Copy linkTweet thisAlerts:
@JonaFeb 12.2003 — I always use variables to make the code more readable myself...

var browser = navigator.appName

var bVer = parseInt(navigator.appVersion)

if((browser == "Microsoft Internet Explorer") && (bVer <= 6) {

document.write("<LINK REL="STYLESHEET" SRC="../file.css" TYPE="text/css">")

} else {

//do whatever else you wanted...

}

That's the way I usually do it, but I always keep the if/else functions down to two lines of code usually.
Copy linkTweet thisAlerts:
@tenorukeauthorFeb 12.2003 — Good point. One of the things thats kept me swiping and repurposing code from people I've worked with over the years (with permission, o'course), rather than writing my own, is the appearance compared to HTML, Styles, etc. Clarity builds confidence, right?

Interestingly, it is a basic competency with action scripting in Flash that gave me the confidence to start writing javascript on my own just recently.
Copy linkTweet thisAlerts:
@JonaFeb 13.2003 — --I do think, however, that it is really cool to see how few lines of code you can get, but arrive at the same result. So I always pull the code as small as I can, like this....

function doSomething() {

if(something){alert(something)} else{alert(somethingelse)}

}

And I usually do everything that way.. I'm just used to reading my personal coding better than others'. I never use all of those tabs, spaces, or return carriages (enters/breaks).

So the above "code" would normally be put...

function doSomething()

{

if(something

{

alert(something)

}

else

{

alert(somethingelse)

}

}

Which is [i]way[/i] long, in my opinion...
Copy linkTweet thisAlerts:
@tenorukeauthorFeb 13.2003 — Cool. I wasn't even aware you could get away with

that much flexibility. Was that true on older browsers (IE 4, Netscape 4x, Netscape 3)?
Copy linkTweet thisAlerts:
@JonaFeb 13.2003 — I'm not sure, but it works in IE4 I believe.
Copy linkTweet thisAlerts:
@AdamBrillFeb 13.2003 — Just to clarify, the condensed version will work in all of the browsers that the long version will work in. Also, here is another way of writing the same thing:
<i>
</i>function doSomething() {
(something)? alert('something'):alert('somethingelse');
}

That does the same thing as the if statement. I just figured I'd throw it out there... ?
Copy linkTweet thisAlerts:
@JonaFeb 13.2003 — Oh yea, duh. Forgot about that ? : thing. Hehe, but you're right. I just don't often use those... just because--well, I don't! ?
×

Success!

Help @tenoruke spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...