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

Posts: 13
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
 
When You Register, These Ads Go Away!
     
Old 04-15-2008, 04:00 AM Re: Changing CSS file with JavaScript
Experienced Talker

Latest Blog Post:
Long time no … erm post?
Posts: 45
Name: Matthew Bagley
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
Who am I | Free Layouts |Website Development | UK SEO
Paramiliar is offline
Reply With Quote
View Public Profile
 
Old 04-15-2008, 02:52 PM Re: Changing CSS file with JavaScript
wayfarer07's Avatar
Will Work for Talkupation

Posts: 575
Name: Abel Mohler
Location: Asheville, North Carolina USA
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?
__________________
Go FREELANCE <=====||=====> Hire Me
wayfarer07 is online now
Reply With Quote
View Public Profile
 
Old 04-16-2008, 01:00 PM Re: Changing CSS file with JavaScript
Average Talker

Posts: 18
Name: TK
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 01:17 PM.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Changing CSS file with JavaScript
 

Thread Tools

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

vB 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.12420 seconds with 13 queries