-2

I was wondering, is there any convention that would allow a style sheet of a page to be used witout using a link or import from that page?

Jim
  • 18,826
  • 34
  • 135
  • 254

3 Answers3

5

Yes, style tags in the page (preferably in the head):

<style>

    /* CSS declarations here */

</style>
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
0

Ok, Here is the simplest thing I could came with:

var style = document.createElement('style');
    style.innerHTML = '#element{color:red}';
document.getElementsByTagName('head')[0].appendChild(style);

THis will dinamically create a style element and appendit to the head. Of course you could do better if you want to use the CSSOM methods like addRule.

http://jsfiddle.net/ekmyR/

Mircea
  • 11,373
  • 26
  • 64
  • 95
0

There is a way you would still use link but not directly from the page.

You could put this in an external javascript file that should be loaded with the page (using jQuery):

$('head').append($("<link rel='stylesheet' type='text/css' href='<FILE.CSS>'/>"));

Hope it helps!

Toyo
  • 314
  • 3
  • 7
  • This still ends up putting a tag into the code so it doesn't seem like this is what the questioner is asking. – RacerNerd May 08 '13 at 21:41