0

Searching for a way to add this code, after the <head> (or some <link stylesheet>) and before <body> is generated (created).

$('body').append('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');

There is no <body> without domready(), so its seems better to use head instead or something else. Append also can be replaced.

There is no direct access for html file edit, thats why I'm using javascript.

Why is it needed? When we use domready() script adds styles after the page is generated, so blocks with new styles jumps from previous decoration to new - not good, thats why we should use another hook.

Anybody knows how to do that?

Thanks.

Happy
  • 881
  • 7
  • 16
  • 32

2 Answers2

2
<script type="text/javascript">
document.write('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');
</script>

Place it wherever you want.

Edit: I don't think what you want is possible, but I'd love to be proven wrong. You don't have any hooks into the DOM parsing stage. And once the DOM is constructed, it's already too late by then for your purposes.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • you don't understand. I have access for javascript file only, which is loaded automatically before the page start to load. This is not ordinary javascript hook, its an userjs for browser – Happy Jun 15 '10 at 10:48
  • Well your question doesn't say that. So you have an HTML page coming from somewhere and you want to inject some styles into it purely through JavaScript and you don't want want to do it on domready, but before that? – Anurag Jun 15 '10 at 10:53
  • @Anurag - want to add styles before is generated/created. Into maybe, or after the to original css files. – Happy Jun 15 '10 at 10:55
  • all placed in section – Happy Jun 15 '10 at 10:56
  • I'm not sure if I should be more upset that you suggested `document.write`, or that it seems to be the correct answer. :-( – Sean McMillan Jan 11 '13 at 18:36
0

Did you try $('body').prepend() or $('head').append() ? You need the DOM to be ready before you can inject html using jquery, so I think you're stuck with document ready.

ScottE
  • 21,530
  • 18
  • 94
  • 131