Jump to content
MakeWebGames

Stylesheet Switcher


Spudinski

Recommended Posts

I bet most of you would use a php script to do this, and it would do a good job at that.

But that is not the only way, you make make it happen in an instant, like Ajax fetches documents, this changes style sheets.

The javascript code that is required isn't much, it is actually just a few lines, so it is a surprise to me that some developers doesn't know how to do this.

The part of this that would take the longest is creating the different style sheets, that takes some time to create a good design.

You need to have a default style sheet already defined in an link element, you will also need to add an id attribute to the element.

<link rel="stylesheet" href="default.css" type="text/css" id="stylesheet">

 

Here is the javascript code, I'm not going split it up and explain what it does, it should be obvious.

function changeStylesheet(location) {
element = document.getElementById('stylesheet');
element.href = location;
return true;
}

 

Here is a complete script that you can use to test this out, with two style sheets.

<html>
<head>
<title>Stylesheet Switcher</title>
<link rel="stylesheet" href="default.css" type="text/css" id="stylesheet">
<script type="text/javascript">
function changeStylesheet(location) {
element = document.getElementById('stylesheet');
element.href = location;
return true;
}
</script>
</head>
<body>
<div class="text">
[b]Choose a stylesheet to load:[/b]

<input type="button" onclick="changeStylesheet('default.css')" value="default.css">
<input type="button" onclick="changeStylesheet('custom.css')" value="custom.css">
</div>


</p>
<div class="test">Test [i]div[/i] element</div>
</body>
</html>

 

body { }

 

body {
background-color: #EEE;
color: #333;
font-family: arial;
font-size: 12px;
}
div.text {
background-color: #CC0000;
padding: 10px;
border: solid 1px #FFF;
color: #FFF;
}
div.text input {
background-color: #CC0000;
border: none;
font-weight: bold;
color: #FFF;
border: dotted 1px #FFF;
}
div.test {
background-color: #FFF;
border: solid 1px #CCC;
color: #666;
font-weight: bold;
padding: 10px;
width: 50%;
text-align: center;
}

 

References:

http://www.w3schools.com/tags/tag_link.asp

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...