Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Changing CSS file with JavaScript
Old 04-14-2008, 08:18 AM Changing CSS file with JavaScript
Novice Talker

Posts: 13
Trades: 0
Is it possible to change the linked stylesheet a page looks at using javascript attached to a hyperlink?

If so how would I go about this?
CubicalPhil is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-15-2008, 05:00 AM Re: Changing CSS file with JavaScript
Experienced Talker

Latest Blog Post:
Long time no … erm post?
Posts: 45
Name: Matthew Bagley
Trades: 0
I have this feature setup on our supply portal Xactio http://www.xactio.co.uk

Code:
    function setcss(TYPE, COLOUR){
        var THEFILE = '<?=WEBSITEURL;?>styles/themes/' + TYPE + '.css'
        var cssNode = document.createElement('link');
        cssNode.setAttribute('rel', 'stylesheet');
        cssNode.setAttribute('type', 'text/css');
        cssNode.setAttribute('href', THEFILE);
        document.getElementsByTagName('head')[0].appendChild(cssNode); 
    }
this is how I did it, all it does is add another tag just below the head start tag which adds a new stylesheet.

If you wanted to change the existing one I am sure this could be done
__________________
Matthew Bagley
Paramiliar Design Studios

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Paramiliar is offline
Reply With Quote
View Public Profile
 
Old 04-15-2008, 03:52 PM Re: Changing CSS file with JavaScript
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Not sure if that can be done, but why don't you just change a class on the body that has different derivatives in a stylesheet that is being linked to already?
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-16-2008, 02:00 PM Re: Changing CSS file with JavaScript
Average Talker

Posts: 18
Name: TK
Trades: 0
I change the href property of the link HTML element.
Code:
<html>
    <head>
        <title>Sheeter Productivity</title>
         <link id="myStyleSheet" href="sheeters.css" type="text/css" rel="stylesheet" />
        <script type="text/javascript">
function getNewCSS(cssIndex) {
    var newStyleSheet;
    switch (cssIndex) {
        case 1:
            newStyleSheet='sheeters_1024x768.css';
            break;
        case 2:
            newStyleSheet='sheeters_1024x768_redlight.css';
            break;
        default:
            newStyleSheet='sheeters.css';
            break;
    }
    document.getElementById("myStyleSheet").href=newStyleSheet;
}
</script>
</head>
<body>  
<a href="javascript:getNewCSS(0); void(0);">1280x1024</a><br>
<a href="javascript:getNewCSS(1); void(0);">1024x768</a><br>
<a href="javascript:getNewCSS(2); void(0);">1024x768 red light</a>
</body>
</html>

Last edited by zxcvbnm60; 04-16-2008 at 02:17 PM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Changing CSS file with JavaScript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59242 seconds with 12 queries