/    Sign up×
Community /Pin to ProfileBookmark

javascript to change stylesheet/browser

Is there a javascript that depending on your type of browser (eg IE and Netscape) allows you to add a specific (different) stylesheet to a page?

In other words: is it possible with javascript to use style1.css for IE and style2.css for Netscape?

Or am I posting in the wrong forum and is there a way to do this simply by adding something in the stylesheet?

thx
Sjemp

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriJan 08.2004 — have a look at my post which is Stylesheet cookie that shows you a way to change cookie just modify the function to detect browser type and then set stylesheet

[URL]http://forums.webdeveloper.com/showthread.php?s=&threadid=24854[/URL]
Copy linkTweet thisAlerts:
@96turnerriJan 08.2004 — <i>
</i>&lt;script language="JavaScript" type="text/javascript"&gt;
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&amp;&amp; a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
if(navName == 'Microsoft Internet Explorer'){
setActiveStyleSheet('style1');
} else {
setActiveStyleSheet('style2');
}
}
}
&lt;/script&gt;
&lt;LINK href="style-black.css" type="text/css" rel="stylesheet" title="style1"&gt;
&lt;LINK href="style-blue.css" type="text/css" rel="alternate stylesheet" title="style2"&gt;

&lt;body onload="setActiveStyleSheet();"&gt;
Copy linkTweet thisAlerts:
@sjempauthorJan 08.2004 — Exactly what i need but this script gives an error :s ?

It only works for the first stylesheet in IE (and there is an error on the page- i think it's saying an object is being expected), but other browsers also display this stylesheet.
Copy linkTweet thisAlerts:
@96turnerriJan 08.2004 — now this works fine

<head>

<LINK href="style1.css" type="text/css" rel="stylesheet" title="style1">

<LINK href="style2.css" type="text/css" rel="alternate stylesheet" title="style2">

<script language="JavaScript" type="text/javascript">

function setActiveStyleSheet(title) {

var i, a, main;

for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

if(a.getAttribute("rel").indexOf("style") != -1

&& a.getAttribute("title")) {

a.disabled = true;

if(a.getAttribute("title") == title) a.disabled = false;

}

}

if (navigator.appName == "Microsoft Internet Explorer") {

setActiveStyleSheet('style1');

} else {

setActiveStyleSheet('style2');

}

}

</script>

</head>



<body onload="setActiveStyleSheet();">



</body>



hope that what u after ?
Copy linkTweet thisAlerts:
@sjempauthorJan 08.2004 — is it me or what? It doesnt work fine with me though.

I get an errorbox in the middle of the screen: Out of memory at line: 7 in IE

btw thx 4 your help 96turnerri

Sjemp
Copy linkTweet thisAlerts:
@96turnerriJan 08.2004 — hmmm so do i know didnt in testing, hmmm strange let me think about it why it could be dont have a clue at the moment
Copy linkTweet thisAlerts:
@PittimannJan 08.2004 — Hi!

The "Out of memory..." is based on the fact, that this:

if (navigator.appName == "Microsoft Internet Explorer") {

setActiveStyleSheet('style1');

} else {

setActiveStyleSheet('style2');

}

is inside the function which is called onload.

Sorry to say, that also the rest doesn't work - don't have time now to fix it...

Cheers - Pit
Copy linkTweet thisAlerts:
@AtReyU_101Apr 23.2007 — I know my answer is about 3 years late ... the answer is here ... http://www.technorealm.co.uk/scripts/browserdetectcss.html ... just for anyone that is busy searching the threads for this ...
×

Success!

Help @sjemp 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...