/    Sign up×
Community /Pin to ProfileBookmark

loading .css files via Javascript?

In my index.html I have a little Javascript that detects the users platform I then want it to dynamically set the filename of the external css file that will be loaded.

This block refelcts my logic and should help illustrate what I am trying to do. I know the syntax is wrong here among other thing. But I think you’ll get the point.

<SCRIPT>
str = navigator.platform;
var computer = str.substring(0, 3)
if(computer == “Win”) {
<link rel=stylesheethref=”windows.css”type=”text/css”>
}
else {
<link rel=stylesheet href=”mac.css” type=”text/css”>}
</SCRIPT>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 17.2004 — Wrap the [I]link[/I] for the style sheet in a [I]document.write[/I].

This should really be done server side.
Copy linkTweet thisAlerts:
@elliottgauthorMay 17.2004 — <SCRIPT>

str = navigator.platform;

var computer = str.substring(0, 3)

if(computer == "Win") {

document.write(<link rel=stylesheethref="windows.css"type="text/css">);

}

else {

document.write(<link rel=stylesheet href="mac.css" type="text/css">);

}

</SCRIPT>

I have tried using document.write() but I can't get that to work. Wouldn't this write into the <body> and not the <head>. I'm pretty new to JS. Any other ideas/input?

Thanks
Copy linkTweet thisAlerts:
@PittimannMay 17.2004 — Hi!I have tried using document.write() but I can't get that to work.[/QUOTE]You will have to put quotes around the string to be written: document.write('<link rel=stylesheet href="mac.css" type="text/css">');Wouldn't this write into the <body> and not the <head>.[/QUOTE]That depends on where you put the script tag...

Cheers - Pit
Copy linkTweet thisAlerts:
@elliottgauthorMay 17.2004 — Thanks a lot. It worked like a charm!!
Copy linkTweet thisAlerts:
@PittimannMay 17.2004 — Hi!

You're welcome! (I just replied to your thread because Fang is offline - actually, it was him to solve your prob)...

Cheers - Pit
×

Success!

Help @elliottg 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.22,
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,
)...