views:

63

answers:

1

I am looking at an issue whereby our ASP.Net MVC page content jumps into position.

If I put a debug point in one of our .js files I can see that the page has already rendered into its initial positions and then the .js files do its stuff and the page redraws into the correct state. This happens pretty quickly but slow enough that a jump on the screen can be seen.

Can I stop the page rendering until everything has finished? Not great at this sort of stuff so any suggestions would be appreciated.

+1  A: 

I am not sure whether I understand what's happening there. You could try by rendering the topmost container element of the page invisible until the script is done.

In the CSS file just assign the display: none CSS property to the container, or better yet, do it in the inline style to the element like this:

<body>
<div class="body invisible" style="display: none;">
Your content
</div>
<script type="text/javascript">
    $("div.body").removeClass("invisible");  //in case JS is present, don't reveal the element until scripts execute.
</script>
<style type="text/css">
    div.invisible {display: block !important;} /*In case no JS support present, reveal the content after page fully loads*/
</style>
</body>

Then in the JS do something like this:

/*Your big JS code*/
$("div.body").css("display", "block"); //sets it visible again after code finishes

You can even place a Loading gif image on the page during this time. Just place a visible container with that background image, and when making the body visible, make that image invisible.

AND, another possible solution to experiment with (not sure about browser compatibility): http://bytes.com/topic/javascript/answers/738083-how-suspend-layout

But it appears that here a poorer solution was accepted: http://stackoverflow.com/questions/2363337/suspend-layout-during-dom-interaction

Alexander
the problem here would be if the browser has disabled javascript, then the page will not be shown...
Reigel
-1 for raping the accessibility
rochal
Yes, true. But there are few such browsers. A workaround would be to place a <style> element at the bottom of the page, which makes the element visible, using the !important directive. ... Agh, Wait, I'll edit and add this detailed workaround :)
Alexander
@rochal, Check out the updated solution, and if you don't find anything seriously wrong with it, please remove the down vote.
Alexander
A better solution would be to have the content visible by default, but render it invisible using inline javascript just after the content appears on the page. You can then render it visible again when the jquery stuff is complete.
Chris
still, this way the content will be rendered, then made invisible by JS, then visible again.
Alexander
@Alexander - It is still wrong. Firstly, <style> tag inside <body> is a wrong HTML according to W3C validator - you should put it in <head>. Secondly - !important does not overwrite inline styles. I would vote it down again if I could ;-)
rochal
@rochal, !important does overwrite inline styles. Check out shakira.us , there you will see after each post details a Facebook and Twitter share button. Look with Firebug at the images styles. Inline styles are overriden by !important in CSS. Check this resource too: http://www.sitepoint.com/blogs/2009/05/27/override-inline-css/W3C validation is something you don't really look after when you want something so specific. Normally everything should be defined in CSS, without JS. So this is an exception, that triggers many more after it.
Alexander
generally, W3C validation should be used for very basic HTML. Facebook integration (<fb: ...) generates errors in W3C validation, but everybody uses them nevertheless. It's silly to go after 100% valid HTML from W3C point of view.
Alexander
When you use $("div.body").removeClass("invisible"); you still leave inline "display:none;" which is still hiding the the content. I mean dude, just paste into .htm file and test it yourself and you will see it is not working correctly.
rochal
Yes, it has inline display: none if jquery removes the invisible class. if jQuery removes the invisible class, it will also remove the inline display: none after all JS has executed. I am assuming that the big JS code lies in a $(document).ready(function() {.....}) (so says jQuery that we must do) so it will execute only after all DOM is loaded, and after that it will show the hidden container.Please, make sure you actually READ the code, that's been present from the very beginning, before you downvote or comment. Cite: `$("div.body").css("display", "block")`
Alexander