dimanche 30 mars 2014

Using Javascript to insert css link into the head of a page


Vote count:

0




I 3rd party site that includes this script:



var foo = "Hello World!";

(function() {
var foo = "Goodbye World!";
document.write("<p class='something'>Inside our anomymous function foo means '" + foo + '".</p>');
})();


I would now like to insert (using pure javascript) an external reference to a style sheet. How can this be done?


Using jquery you could do it like this:



var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "style.css"
});
css_link.appendTo('head');


however I need to keep this pure javascript.



asked 28 secs ago

Spike

683





Aucun commentaire:

Enregistrer un commentaire