/    Sign up×
Community /Pin to ProfileBookmark

How to load different .CSS files with Javascript?

Hello,

I’m new here, and I have got a problem with some Javascript-code.

My English isn’t very well… but I try to make good senteces, sorry for that.

First the problem: I would like to load a .CSS file depending of the resolution of a computer.

This is what I have done:

<head>

<SCRIPT LANGUAGE=”JavaScript”><!–

function loadFile(){
if(screen.width == 1024 && screen.height == 768){
<link href=”menu.css” rel=”stylesheet” type=”text/css”>
}
else if(screen.width == 1280 && screen.height == 800){
<link href=”menu2.css” rel=”stylesheet” type=”text/css”>
}
}

//–></script>

</head>

<body onLoad=”loadFile()”>

Can somebody tell me why this isn’t working?

I hope I will have got a quick reply.

Regards,

Daboytommy87

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 04.2005 — [code=php]<head>

<script type="text/JavaScript">
if(screen.width == 1024 && screen.height == 768){
document.write('<link href="menu.css" rel="stylesheet" type="text/css">');
}
if(screen.width == 1280 && screen.height == 800){
document.write('<link href="menu2.css" rel="stylesheet" type="text/css">');
}
</script>

<noscript><link href="menu.css" rel="stylesheet" type="text/css"></noscript>

</head>[/code]
Copy linkTweet thisAlerts:
@LeeUNov 04.2005 — You can also try it this way. Put a link to the default style sheet:

[CODE]
<link rel="stylesheet" type="text/css" media="screen" href="size1024.css">
[/CODE]


Then add this script:

[CODE]
<script type="text/javascript">
<!--
if (screen.width < 1024)
link = document.getElementsByTagName("link")[0];
link.href = "size800.css";
-->
</script>
[/CODE]


You could also adjust it to allow for other style sheets.
Copy linkTweet thisAlerts:
@daboytommy87authorNov 06.2005 — Thanks for the options, it works ?.
×

Success!

Help @daboytommy87 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.2,
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,
)...