views:

380

answers:

5

I have a jQuery script that changes the src attribute of an <img> and a few other page elements.

My script is hooked to the document-ready callback using $(). When I refresh the page I see the original content for half a second. I want to avoid that.

Is there a way for my script to execute after the DOM is ready but before it is rendered?

+3  A: 

I don't think it's possible. These days, browsers begin rendering the UI before the DOM has finished building.

Joel Coehoorn
Correct. For example Firefox has a few hidden `about:config` settings to control that, like `nglayout.initialpaint.delay` which is the time in milliseconds before Firefox starts rendering while still downloading (default 0.25 seconds), and `content.notify.interval` being the time in microseconds (1/1000 millisecond) between reflows (default 0.12 seconds).
Arjan
I accept this answer because it is the most accurate: In short, it's not possible to execute code before rendering starts. But thanks to everyone else for the suggested workarounds.
Sly
+6  A: 

Not really, but you can fake it really well. Try setting the image to display='none'. After you've set the src attribute, you can reveal the image with $('#myImage').show().

Chris Pebble
Note the risk here: if a user has JS turned off, they won't be able to see that content. If that's an acceptable risk, this is the correct answer.
mgroves
Very true, but in this case he's already crossed that bridge by using JS to set the src of the image.
Chris Pebble
@Chris: I'm not allowed to change the page's HTML or the stylesheet. The author of the page has included a <script> to "custom.js" in the page. I have to provide the custom.js script and do whatever I can with that. So I need a solution where everything happens in my script.
Sly
That would complicate it a little. Is your JS file getting loaded after the image tag or before (i.e. is it on the header or footer of hte page?)
Chris Pebble
It is in the header and it is the first <script> tag.
Sly
+1  A: 

hide the content you don't want to show with the CSS property:

display: none;

or if you don't want to mess up the layout of the page:

visibility: hidden;

It is kinda hard to delay the entire process of rendering the UI because a web page's performance is based on how fast the UI is rendered. tags usually block the rendering of the UI as the Javascript interpreter goes through your code, but that can happen so fast that you won't get the functionality you desire.

strife25
+1  A: 

Btw, you can generally access an element immediately after it's closing tag. On larger, composite, elements (like divs) it may have already started rendering, though.

<img src="nothin.jpg" id="dynamicImage" />
<script type="text/javascript">$('#dynamicImage').hide();</script>
Richard Szalay
you could delay a situation with the image by creating the <img> tag, but not applying the src attribute yet, then run your JS code, then give a value to the src attribute to start downloading.
strife25
hide() was just an example. You could also change the src element, yes.
Richard Szalay
+2  A: 

I would hide the body immediately after rendered...

<body>
<script type="text/javascript">document.body.style.display = 'none';</script>

And then show it after your jQuery code executes...

 $(document).ready(function() {
   // Whatever you gotta do here
   document.body.style.display = '';
 });

This will ensure your site is functional with Javascript disabled.

Josh Stodola