views:

60

answers:

2

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.

+2  A: 
<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
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
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
question is updated
Happy
@Anurag - want to add styles before <body> is generated/created. Into <head> maybe, or after the <link stylesheet> to original css files.
Happy
all <link stylesheet> placed in <head> section
Happy
Your update to the question has cleared things up.
Anurag
A: 

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