admin管理员组

文章数量:1296471

I want to change the used CSS File (<link href="..." />) dynamically using only javascript and to save changes in cookies.

This is a jQuery version that does what I want (ref), but how can I do this in javascript?

if($.cookie("css")) {
    $("link").attr("href",$.cookie("css"));
}
$(document).ready(function() {
    $("#nav li a").click(function() {
        $("link").attr("href",$(this).attr('rel'));
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });
});

Thank you in advance.

I want to change the used CSS File (<link href="..." />) dynamically using only javascript and to save changes in cookies.

This is a jQuery version that does what I want (ref), but how can I do this in javascript?

if($.cookie("css")) {
    $("link").attr("href",$.cookie("css"));
}
$(document).ready(function() {
    $("#nav li a").click(function() {
        $("link").attr("href",$(this).attr('rel'));
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });
});

Thank you in advance.

Share Improve this question edited Jan 22, 2013 at 17:03 AndraD 2,8506 gold badges39 silver badges48 bronze badges asked Sep 1, 2012 at 16:21 VahidVahid 3,4424 gold badges36 silver badges71 bronze badges 5
  • 2 jQuery is JavaScript. You access cookies via document.cookie. – Pointy Commented Sep 1, 2012 at 16:25
  • Thank you. I don't want to load jQuery. So, how about the CSS part? – Vahid Commented Sep 1, 2012 at 16:27
  • 1 @Pointy Unless the it is an HTTP only cookie :) – PeeHaa Commented Sep 1, 2012 at 16:28
  • Well yes but then jQuery couldn't do it either! – Pointy Commented Sep 1, 2012 at 16:29
  • HTTP cookie is ok for now, but the other part? – Vahid Commented Sep 1, 2012 at 16:33
Add a ment  | 

2 Answers 2

Reset to default 5

maybe this could help you..

(function() {
    var e = document.createElement('link'); 
    e.href = document.location.protocol + '//example./file.css';
    e.type = 'text/css';
    e.rel = 'stylesheet';
    e.media = 'screen';
    document.getElementsByTagName('head')[0].appendChild(e);      
}());

Edit, full JavaScript without jQuery

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

document.addEventListener('DOMContentLoaded',function(){

    if(readCookie('css')){
        var e = document.getElementById('test-css'); // <link href="..." id="test-css"/>
        e.href = readCookie('css'); 
    }

    var element = document.getElementById('change-css'); // <a herf="#" id="change-css" rel="file.css">Click Here</a>
    element.addEventListener('click', function (event) { 
        var e = document.getElementById('test-css');
        e.href = this.rel;
        if(readCookie('css')){  
            eraseCookie('css');     
        }
        createCookie('css',this.rel,365); 
        event.preventDefault(); 
    }, false);
})

Read: http://www.quirksmode/js/cookies.html

And: http://www.hunlock./blogs/Howto_Dynamically_Insert_Javascript_And_CSS

First, lets make an example html layout.

<html> <!--#include javascript.js styles.css-->
    <body>
        <span>sometext</span>
        <p>somemoretext</p>
    </body>
</html>

Next, lets make an example css layout.

span {
    color: red;
}

p {
    color: blue;
}

body {
    background: black;
}

Now is the javascript. Before we do anything, cookies cannot store css information. We will use cookies names to determine the type of style to load. The quirksmode resource above lets us easily use cookies.

if(readCookie("newStyle")) {
    // do something
} else {
    // do something else
}

In this case, we want to change the styles. We can use two methods: document.element.style.property or appendChild with the href of another CSS sheet. In this example, we use document.element.style.property.

if(readCookie("newStyle")) {
    eraseCookie("newStyle")
    document.getElementsByTagName("span")[0].style.color = "purple"
    document.getElementsByTagName("p")[0].style.color = "pink"
} else {
    createCookie("newStyle",0000,60)
}

Live Example: http://jsfiddle/5HGsH/3/

Refresh the page multiple times to see different colored words.

本文标签: jqueryJavascript Change CSS File DynamicallyCookieStack Overflow