admin管理员组

文章数量:1335371

I am trying to modify the body of a page before the DOM is displayed using JavaScript or jQuery.

I'm able to do it fairly well with:

jQuery(document).ready(function() {          

   jQuery('body').load('?variation-id='+dynamic_id_number, function() {});

});

But the problem is that the page renders the old body content for a split second before showing the new body content.

Is there a way I can detect when the DOM is loaded and modify it before it is printed?

I am trying to modify the body of a page before the DOM is displayed using JavaScript or jQuery.

I'm able to do it fairly well with:

jQuery(document).ready(function() {          

   jQuery('body').load('?variation-id='+dynamic_id_number, function() {});

});

But the problem is that the page renders the old body content for a split second before showing the new body content.

Is there a way I can detect when the DOM is loaded and modify it before it is printed?

Share Improve this question edited May 1, 2013 at 21:15 Simon Adcock 3,5623 gold badges28 silver badges42 bronze badges asked May 1, 2013 at 21:05 atwellpubatwellpub 6,03012 gold badges40 silver badges53 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

If you're waiting for document.onready, it'll wait for the dom to be ready ;). Don't use it, just embed it (your script) in your head tag.

The DOM is fully visible at the same instant it is fully loaded.

If you want to avoid this, I suppose you could just hide the body with CSS and show it again when you're done:

CSS:

body { display: none; }

jQuery:

jQuery(document).ready(function() {          
    jQuery('body').load('?variation-id='+dynamic_id_number, function(){
        jQuery('body').show();
    });
});

You can hide BODY element contents or BODY element itself via CSS:

HTML.hidden > BODY {
    display: none;
}

hidden class should be added right inside HEAD section (while <body> opening tag is not even encountered by browser yet):

$(document.documentElement).addClass('hidden');

and then unhide it in ready() handler:

$(document).ready(function() {
    $(document.documentElement).removeClass('hidden');
});

Hiding BODY element statically is bad for cases when JS execution is disabled.

Instead of using $(document).ready(..), just drop the script directly onto the page, preferably after the element being referenced has been defined. Example:

<div id="foo">Bar</div>
<script>$("#foo").html("Loaded!");</script>

Its not pretty, but it does the job.

If you don't have access to the page's source code, you can try this:

var css = 'body { display:none; background-color:white; }',
style = document.createElement('style');
style.type = 'text/css';

if(style.styleSheet)
{
    style.styleSheet.cssText = css;
} else
{
    style.appendChild(document.createTextNode(css));
}

head = document.head || document.getElementsByTagName('head')[0],
head.appendChild(style);

本文标签: javascriptModifying body HTML content before DOM is loaded and displayed in browserStack Overflow