Skip to content

Instantly share code, notes, and snippets.

@ali-master
Created December 31, 2016 16:50
Show Gist options
  • Save ali-master/450caded6bd8b393f586b398975c44d8 to your computer and use it in GitHub Desktop.
Save ali-master/450caded6bd8b393f586b398975c44d8 to your computer and use it in GitHub Desktop.
Add CSS File To DOM

Add CSS File To DOM

function addCss(data) {
    (function (e) {
        e.setAttribute("rel",  "stylesheet");
        e.setAttribute("type", "text/css");
        e.setAttribute("id",   data.id);
        e.setAttribute("media", "all");
        e.setAttribute("href", data.src);
        
        document.getElementsByTagName("head")[0].appendChild(e);
    })(document.createElement("link"));
}

Usage

var data = {id: "screen", src: "//3px.ir/screen.css"}
addCss(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment