08 March 2016

Add or remove a CSS stylesheet to HTML using plain vanilla Javascript

This little function will attach or delete a new stylesheet to your html page using good old JS. The second parameter is optional so it will toggle on or off the stylesheet, depending on it's current setting. The function will return true or false (1 or 0), depending on weather the style is on or off. Enjoy!


function toggleStylesheet(href,onoff){
    var existingNode=0 //get existing node:
    for(var i = 0; i < document.styleSheets.length; i++){
        if( document.styleSheets[i].href && document.styleSheets[i].href.indexOf(href)>-1 ) existingNode = document.styleSheets[i].ownerNode
    }
    if(onoff == undefined) onoff = !existingNode //toggle on or off if undefined
    if(onoff){ //TURN ON:
        if(existingNode) return onoff
        var link  = document.createElement('link');
        link.rel  = 'stylesheet';
        link.type = 'text/css';
        link.href = href;
        document.getElementsByTagName('head')[0].appendChild(link);
    }else{ //TURN OFF:
        if(existingNode) existingNode.parentNode.removeChild(existingNode)
    }
    return onoff
}

//EXAMPLES:

toggleStylesheet('myStyle.css') //toggle myStyle.css on or off

toggleStylesheet('myStyle.css',1) //add myStyle.css

toggleStylesheet('myStyle.css',0) //remove myStyle.css

8 comments:

Meiqing Xu said...

coach factory outlet
nike free run flyknit
coach outlet
ugg outlet store
oakley sunglasses
adidas super color
pandora charms
nike shoes for men
converse shoes
christian louboutin
20161228caiyan

Gembiraria said...

hajar wess omz AGUNGQQ Agen DominoQQ, BandarQ, Capsa Susun, QQ Online Terpercaya di Indonesia

Agen Bandar66 Online Judi Sakong Capsa BandarQ Domino99 Keris99

ituCasino Agen Bola Sbobet Piala Dunia 2018 Live Casino Online

Judi Sbobet LiveCasino 338a Agen Bola Piala Dunia Online KiosCasino

daftar poker online said...

Mrjudibola.com agen bola terpercaya

Mrjudibola.com Bandar bola terpercaya

Daftar Poker Online

Poker Online Indonesia

Situs Berita terkini

Detik Siang

agen domino qq terpercaya said...

BwinQQ.casino agen dominoqq terpercaya

BwinQQ.casino situs dominoqq terpercaya

BwinQQ.casino situs poker online terpercaya

BwinQQ.casino agen poker terpercaya

bandarqiu99.info situs domino99 terpercaya

AGUNGQQ Agen DominoQQ, BandarQ, Capsa Susun, QQ Online Terpercaya di Indonesia said...

AGUNGQQ Agen DominoQQ, BandarQ, Capsa Susun, QQ Online Terpercaya di Indonesia

Agen Bandar66 Online Judi Sakong Capsa BandarQ Domino99 Keris99

ituCasino Agen Bola Sbobet Piala Dunia 2018 Live Casino Online

Judi Sbobet LiveCasino 338a Agen Bola Piala Dunia Online KiosCasino

obat aborsi ampuh said...

cara menggugurkan kandungan

cara ampuh menggugurkan kandungan

obat aborsi

obat aborsi ampuh

cara menggugurkan kandungan said...

allways good ide for write high quality content in this blog
Cara Menggugurkan kandungan

Obat Aborsi

obat aborsi

cara menggugurkan kandungan

obat aborsi

cara menggugurkan kandungan

cara ampuh menggugurkan kandungan

cara menggugurkan kandungan

obat aborsi

obat penggugur kandungan

chenlili said...

coach outlet
michael kors outlet
nfl jerseys wholesale
christian louboutin outlet
coach outlet online
canada goose outlet
canada goose outlet store
canada goose outlet
christian louboutin sale
pandora outlet
shenyuhang20180608

Post a Comment

Cron Design Studio: Dublin based web design & software development