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

15 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

Julie T. Kirwan said...

https://titaniumbackup.pro/apk/
https://esfileexplorer.pro/apk/
https://beachbuggyracingmod.xyz/apk/

Amy Wong said...

what about your finances not bad but everything is just gorgeous Do you want to share with you how to make money sitting on the Internet come here crackajack video poker machines I wish you good luck earning head

Belago said...

Качественная лед лента разных цветов, герметичные и нет, я обычно беру в Экодио

على جمال said...

شركة مكافحة الحشرات بالرياض
شركة مكافحة النمل الابيض بالرياض
شركة مكافحة النمل الاسود بالرياض
شركة مكافحة الفئران بالرياض
شركة مكافحة الصراصير بالرياض
شركة مكافحة البق بالرياض
شركة مكافحة الوزغ بالرياض
شركة مكافحة الثعابين بالرياض
شركة رش دفان بالرياض
شركة رش مبيدات بالرياض

Unknown said...

This is a wonderful article, obat aborsi cytotec asli Given so much info in it, These type of articles keeps the users obat aborsi surabaya interest in the website, and keep on sharing more ... good luck cara menggugurkan kehamilan.

yanmaneee said...

nike react
yeezy boost 350
off white nike
christian louboutin shoes
hermes birkin
hermes handbags
kd 11
lebron shoes
yeezy
michael kors handbags

Sidaring said...

Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.
Aplikasi Dapodikdasmen Terbaru
Sidaring

Post a Comment

Cron Design Studio: Dublin based web design & software development