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 badges5 Answers
Reset to default 4If 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
版权声明:本文标题:javascript - Modifying body HTML content before DOM is loaded and displayed in browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372322a2462475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论