jQuery add stylesheet if not loaded

Sam Deering
Tweet

Code snippet quicky. Use jQuery to add a setylesheet if it’s not already loaded.

var cssUrl = 'css/style.css';
if($('link[rel*=style][href="'+cssUrl+'"]').length==0)
{
    $("head").append('<link rel="stylesheet" type="text/css" href="'+cssUrl+'"/>');
}

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments