tags:

views:

78

answers:

2

I'm using jQuery to modify the style of some pages in a large set of existing Web pages. This works, but the problem I have is that, for longer pages, the page is briefly visible with the wrong styles before the javascript kicks in and transforms the page.

I'm using this kind of thing (the alerts are just to emphasize the problem):

$(document).ready(function(){
   ...
   alert("at this point the page is visible unstyled");
   $('body').addClass('myStyle');
   alert("now page looks like I want it to");
   ...
}

How do I prevent the browser from displaying the page until $(document).ready(function() completes?


EDIT
Note: I can't change the HTML of the pages (they're not mine). All I can do is swap in a replacement JavaScript file and work with what's already in the HTML

+2  A: 

Wrap everything with <div id="wrap" style="display: none"></div> and then at the end of your domReady handler put $('#wrap').css('display', '');

In case you can not change HTML, try this in your script:

$('body').css('display', 'none');

$(document).ready(function(){
   ...

   $('body').css('display', '');
}
Marko Dumic
Sorry - I should have said: I can't change the HTML of the pages (they're not mine). All I can do is swap in a replacement JavaScript file and work with what's already in the HTML.
Alistair Christie
Just about to post the same general answer. I added the style to the body tag and just used $("body").show(); to remove the display:none;. Quick test showed it to work fine in both FF 3.6 and IE8.
Philip T.
Nope. Doesn't work, I just tested it. The page is still displayed (*very* briefly) before $('body').css('display', 'none'); kicks in and hides the body. Note: on a small page this isn't visible, but on a large page, there's a flicker as one set of styles changes to another. What's happening is: 1. Display the page with it's original styles; 2. Hide the body. 3. Show the body with the new styles.
Alistair Christie
@Alistair: if that is the case, it means that your javascript is loaded after the `body` (or at least some part of it). If you can't move your script upper in the page (preferably into the `head`) then there's nothing else you can do. (Even if you can modify a CSS file i would **not** suggest you put `body { display: none }` in it)
Marko Dumic
:-( ok, that's sounding like there's no way round this then. My ref to the javascript file *is* in the head, but the javascript processing is definitely happening (or at least continuing) after the page has loaded and is displayed (albeit momentarily) in the browser. I was hoping there was a way of telling the browser not to do anything (i.e. just continue displaying the previously loaded page) until the new page is ready to be displayed.
Alistair Christie
A: 
var blanky = document.createElement("div");  
blanky.style.width = "100%";  
blanky.style.height = "100%";  
blanky.style.position = "absolute";  
blanky.style.top = "0px";  
blanky.style.left = "0px";  
blanky.style.backgroundColor = "white";  
blanky.id = "blanky";  
document.body.appendChild(blanky);  

Now after you loaded all your stuff, do this;

document.getElementById("blanky").style.display = "none";

Do this stuff as soon as the body element is loaded. If your script is inside the body element anyway, don't append any events. If it's in head, you should use the ready event for example.

This also allows you to put virtually anything into the div, say, a loading screen for example!

sinni800
This sort of works in Firefox (3.6), by pushing the existing page content below the fold, beneath a big white space above. But in IE8 it doesn't work at all.
Alistair Christie