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

23 comments:

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

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

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.

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

محمد على said...

شركة تسليك مجارى بالرياض
شركة تنظيف كنب بالرياض
شركة تنظيف خزانات بالرياض
شركة عزل خزانات بالرياض

kayan web said...

Al-Najah Company is one of the most distinctive things about Al-Ahsa cleaner company. Cleanliness of furniture, and some individuals are responsible for the operation of devices, whether pumping devices or suction, and there is a team responsible for the process of sterilization air conditioners , Our team is considered one of the best teams because of its vast experience over many years in this field, skill and high efficiency, and fast and perfect performance. The nature of the work of the team and what may be seen when carrying out its mission, and we have a team of women to be at the service of our valued customers so as not to worry about the entry of the team of men to come home in the presence of women and housewives.
شركة تنظيف بالاحساء
شركة تنظيف مكيفات بالاحساء
شركة تسليك مجارى بالاحساء
شركة مكافحة حشرات بالاحساء
https://www.a-lnagah.com

kayan web said...

شركة تنظيف بالمدينة المنورة
شركة نظافة بالمدينة المنورة
https://www.cleaningcompanyusa.com/

kayan web said...

Welcome to the official website of our company to clean apartments, houses, villas and shops in Saudi Arabia This is because we are the best home services company at the moment and by the testimony of our valued customers, the workers who work in the company have a very high experience in the field of cleaning in general, as well as we will give you cleanliness of the highest quality and the lowest prices with the testimony of customers, our goal is to satisfy our customers to the highest degree possible and thanks to God always First of all, thanks to the workers and technicians, we were able to satisfy all the customers who contracted with the company and contacted them, because we specialize in cleaning, sewerage and transport of luggage just contact us and you will notice that your house as if it is completely new from the intensity of cleanliness.
شركة الصفرات للتنظيف بالرياض
شركة الصفرات لتنظيف الخزانات بالرياض
شركة الصفرات لمكافحة الحشرات بالرياض
شركة الصفرات لنقل الاثاث بالرياض
شركة الصفرات لكشف التسربات بالرياض
شركة تنظيف بالمدينة المنورة
شركة تنظيف بينبع
https://www.cleanriyadh.com/

kayan web said...

Due to the level of quality provided by our company Al-Safa Company in Al-Ahsa شركة تنظيف بالاحساء of cleaning and cleaning services of air conditioning and sewage and insect control on all household services that we do,شركة تنظيف مكيفات بالاحساء and taking our prices suitable for all segments of society, has become the best and most important company in the field of cleaning. شركة تنظيف خزانات بالاحساء In order to maintain our success and progress, the company strives to continue along the same lines, through continuous development and the constant pursuit of following all that is best for the masters of customers. شركة مكافحة حشرات بالاحساء So, never hesitate our dear customer to contact us if you want daily, weekly or monthly cleaning for your home, your garden or your company, شركة تسليك مجارى بالاحساء or even want any kind of cleaning services, we will promptly meet your request with perfect quality and low cost .
شركة الصفرات لكشف التسربات بالرياض
شركة الصفرات للتنظيف بالرياض
شركة الصفرات لتنظيف الخزانات بالرياض
https://www.al-safaa.com/

kayan web said...

شركة تنظيف فلل بالرياض Perfect cleaning and cleaning of the villa's rooms and the use of a vacuum cleaner to remove dust and soil.شركة تنظيف بالرياض The beauty and cleanliness of the place gives the use of steam cleaning machines that remove stains accurately and quickly and are used to clean canes and boards.
شركة تنظيف بالخرج Carpets, carpets and ceramics. If you are unable to execute these tips from شركة تنظيف منازل بالخرج .
شركة مكافحة الحمام بالرياض We provide you with a range of tips that prevent the nesting of the bathroom on the surface of your home you clean the surface and balconies from the remnants of the grain and clean it from waste and used furniture
شركة تنظيف منازل بالمدينة المنورة We have the ability to make your home an oasis of cleanliness and beauty. We have a dedicated team to clean every part of your home, but we are keen to provide important tips to keep your house clean for as long as possible. شركة تنظيف بالمدينة المنورة Use a vacuum cleaner to get rid of accumulated dust on a daily basis. Cleaning the glass home facades provides a perfect look for the home Provide you with a great service in cleaning your house if you can not do this hard work and effort شركة نقل عفش بالمدينة المنورة .
شركة تنظيف بينبع Provide you with a distinctive service in all types of private cleaning Carpet carpet cleaning using the steam cleaning machine makes it clean and fully sterile.
شركة نقل اثاث بينبع It is an ideal choice to carry out your home furniture without any effort or hardship. But if you want to do it yourself, arrange the clothing bags for your family so that you can remove the tires and wooden rooms easily. Transportation. Many things you can do .
شركة تنظيف منازل بمكة It is the best choice for you to clean the perfect parts of your home - Carpet cleaning and carpeting the best types of steam cleaning machines or sent to Cleaning and cleaning of beds and boards and putting mattresses on them to prevent the accumulation of dust and stains on them You must use high quality detergents to ensure high quality cleaning of all the items and parts of your home You need a daily cleaning of the kitchen and bathrooms to give a recovery and confidence in the cleanliness of the whole house شركة تنظيف خزانات بمكة Able to clean your home without bothering you or get tired at great cheap prices.
Visit our website
https://www.allsaad.com/

Praneeth M said...

It's very useful blog post with inforamtive and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now.Find this real time DevOps Training and great teaching. Join now on Selenium Training online course. Upskill career with Tableau training by crs info solutions. Latest trending course is Salesforce Lightning training with excellent jobs.

for a said...

Great Article Image Processing Projects Deep Learning Projects for Final Year JavaScript Training in Chennai JavaScript Training in Chennai The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

Dellam Arlowe said...

Thank you for this wonderful post. It is very informative and useful. I would like to share something here too.carpet steam cleaning

Anonymous said...

It is quite helpful that you have offered this information on your blog. We sincerely appreciate the effort you put into your article, and it helps us, too. Thank you for sharing this information.cms development

Anonymous said...

The fact that you provided this knowledge on your blog is quite beneficial. We genuinely appreciate the time and work you put into writing your article, which also benefits us. I appreciate you giving this knowledge.web development services

Anonymous said...

You have given me a very complete post about. I and those who like it think it's a fantastic piece. Your sharing of this information with us is greatly appreciated.electronic signature

Post a Comment

Cron Design Studio: Dublin based web design & software development