开发者

Site themes; how would I make this extract of code work?

开发者 https://www.devze.com 2023-01-25 08:55 出处:网络
Basically I need to know how to make this work. I removed one of the less than signs due to it hiding the code.:

Basically I need to know how to make this work. I removed one of the less than signs due to it hiding the code.:

<script type = "text开发者_如何转开发/javascript">
link rel="stylesheet" type="text/css" href="..\CSS\stylesheet2.css" />
</script>


Not really sure what you mean, but if you want to load the stylesheet from javascript you would do like this:

function load()
{        
    var head = document.getElementsByTagName("head")[0];

    var link = document.createElement("link");
    link.href = "style/style.css";
    link.type = "text/css";
    link.rel = "stylesheet";

    head.appendChild(link);
}
window.onload = load;

If you want something conditional, you can implement that into the code


<script type="text/javascript">


if(yourBoolean1) {

var newSS=document.createElement('link');

newSS.rel='stylesheet';

newSS.href='data:text/css,'+escape(styles);

document.getElementsByTagName("head")[0].appendChild(newSS);

}


</script>


Best way would be with a server-side script like PHP, so the desired theme will be loaded from the start. This also makes cookie handling a lot easier.

Failing that, you would need to read document.cookie and find the value, then bring it in:

<script type="text/javascript">
    var c = document.cookie;
    if( c.indexOf("theme")>-1) {
        c = c.split(";");
        while(c[0].indexOf("theme")==-1) c.shift();
        c = c[0]; // we now have the part of the cookie with the theme
        c = c.split("=")[1]; // this is the value of said cookie
        var l = document.createElement('link');
        l.type = 'text/css';
        l.rel = 'stylesheet';
        l.src = '..\CSS\stylesheet'+c+'.css';
        document.getElementsByTagName('head')[0].appendChild(l);
    }
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消