views:

161

answers:

3

Hi!

Is there any way to suspend execution of javascript until whole page is loaded in a browser? I am updating a div at the bottom of the document using AJAX but when I click this link before whole page is loaded I get an error...

<%= link_to_remote 'Update', :update => 'div_at_the_bottom', :url => { :usual_stuff } %>
+1  A: 

If you go for a JavaScript library like jQuery you can easily bind any javascript code to the DOM ready event and avoid issues of this kind.

code_burgar
OP is not "binding any javascipt code". OP is using `link_to_remote` which your answer doesn't address. At all.
Crescent Fresh
+3  A: 

The simplest way is to do everything on window.onload:

window.onload = function() {
  // do everything here
}

You could also put your javascript right at the end of the page, before </body>. That way whatever objects you're operating on are guaranteed to be in the DOM.

As others have said, some JS frameworks have a nice method of running JS as the DOM loads. The main advantage of this is you don't get the JS equivalent of "flash of unstyled content", i.e. seeing an element in one (possibly ugly) form, then changing once the full page has loaded.

DisgruntledGoat
that event fires only after the whole document has been loaded, including all images and what not, which is kind of an overkill in most situations
code_burgar
True, but the object the OP is modifying is at the bottom of the page so the impact is minimal I assume.
DisgruntledGoat
It's even part of Google's "best-practices" for web development at http://code.google.com/speed/articles/optimizing-opensocial-gadgets.html -- "Place Javascript as late in the page as possible. Gadgets and webpages are loaded and rendered from the top down. Let user visible content like HTML, images and CSS be higher priority in download order."
Michael Paulukonis
And how, pray tell, do you jam rails' `link_to_remote` into `window.onload`?
Crescent Fresh
+2  A: 

Disable the generated link and enable it when the dom has loaded:

  1. Disable the link:

    <%= link_to_remote 'Update', 
            { :update => 'div_at_the_bottom', :url => { :usual_stuff } },
            { :disabled => 'disabled', :id => 'update-trigger' }
    %>
    

    (note that to be able to pass this extra option to link_to_remote you need to make the [2nd] "options" hash explicit by wrapping it with curlies {}. Extra help on this)

  2. Then somewhere (anywhere after the prototype js lib has been included) enable the link when the DOM has loaded (this is similar to what everyone has been saying to do so far):

    <script>
    $(document).observe('dom:loaded', function() {
        $('update-trigger').disabled = false
    })
    </script>
    

This solution is also semantically agreeable, if that matters to you (it does to me).

Crescent Fresh
That's exactly what i wanted.BTW 'disabled' tag doesn't work with <a> but I get the whole idea.Thanks
Kylo
@Kylo: by golly you are right. http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.12.1 I've used this attribute on anchors successfully in apps built for IE and Firefox. Damn it.
Crescent Fresh