Michael Stillwell
2008-Apr-14 20:34 UTC
evaluate when XHR request completes *and* dom:loaded?
document.observe("dom:loaded", ...) doesn''t seem to fire if the DOM is already loaded. Is there some way to trigger an Ajax.Request and have onSuccess happen if and only if the DOM is loaded? var ajax = new Ajax.Request(url, { onSuccess: function(res) { // run this, but only if DOM is loaded } }); --M. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Gregory
2008-Apr-14 20:42 UTC
Re: evaluate when XHR request completes *and* dom:loaded?
There''s always the poor-man''s method. At the very end of your DOM, insert a script var DomLoaded = true; .... TAG On Apr 14, 2008, at 2:34 PM, Michael Stillwell wrote:> > document.observe("dom:loaded", ...) doesn''t seem to fire if the DOM is > already loaded. Is there some way to trigger an Ajax.Request and have > onSuccess happen if and only if the DOM is loaded? > > var ajax = new Ajax.Request(url, { > onSuccess: function(res) { > // run this, but only if DOM is loaded > } > }); > > > > --M. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Stillwell
2008-Apr-14 20:58 UTC
Re: evaluate when XHR request completes *and* dom:loaded?
Sorry, to clarify my code fragment, I want to wait until the DOM is ready before running a chunk of code: var ajax = new Ajax.Request(url, { onSuccess: function(res) { // run this once the DOM is ready } }); I can''t add document.observe("dom:loaded", ...) to the start of the onSuccess function because it appears to not run if the DOM is already ready, which makes perfect sense from an event based point of view. I guess part of the problem is that dom:loaded is more of a flag than an event... (It can never happen more than once, for example.) --M. On Apr 14, 9:42 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> There''s always the poor-man''s method. At the very end of your DOM, > insert a script > > var DomLoaded = true; > > .... > > TAG > > On Apr 14, 2008, at 2:34 PM, Michael Stillwell wrote: > > > > > document.observe("dom:loaded", ...) doesn''t seem to fire if the DOM is > > already loaded. Is there some way to trigger an Ajax.Request and have > > onSuccess happen if and only if the DOM is loaded? > > > var ajax = new Ajax.Request(url, { > > onSuccess: function(res) { > > // run this, but only if DOM is loaded > > } > > }); > > > --M.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
var callback = function(){ //do stuff }; if(document.loaded) callback(); else document.observe(''dom:loaded'', callback); - JDD --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Or wrap document.observe and have it fire instantly if document.loaded: http://pastie.caboo.se/180748 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
if (document.loaded) { // do stuff } - kangax On Apr 14, 4:58 pm, Michael Stillwell <ithinkihavea...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Sorry, to clarify my code fragment, I want to wait until the DOM is > ready before running a chunk of code: > > var ajax = new Ajax.Request(url, { > onSuccess: function(res) { > // run this once the DOM is ready > } > }); > > I can''t add document.observe("dom:loaded", ...) to the start of the > onSuccess function because it appears to not run if the DOM is already > ready, which makes perfect sense from an event based point of view. I > guess part of the problem is that dom:loaded is more of a flag than an > event... (It can never happen more than once, for example.) > > --M. > > On Apr 14, 9:42 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: > > > There''s always the poor-man''s method. At the very end of your DOM, > > insert a script > > > var DomLoaded = true; > > > .... > > > TAG > > > On Apr 14, 2008, at 2:34 PM, Michael Stillwell wrote: > > > > document.observe("dom:loaded", ...) doesn''t seem to fire if the DOM is > > > already loaded. Is there some way to trigger an Ajax.Request and have > > > onSuccess happen if and only if the DOM is loaded? > > > > var ajax = new Ajax.Request(url, { > > > onSuccess: function(res) { > > > // run this, but only if DOM is loaded > > > } > > > }); > > > > --M.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Gregory
2008-Apr-14 22:15 UTC
Re: evaluate when XHR request completes *and* dom:loaded?
What he said. This is what I was getting at, although I haven''t been up on the Prototype changes enough to have known that newer versions extend the document object with the useful document.loaded attribute. More to your situation, just plug jdalton''s code inside your onSuccess declaration: var ajax = new Ajax.Request(url, { onSuccess: function(res) { // jdalton''s code: var callback = function() { // Do stuff; can use res as an in-scope variable. // Be careful if this callback could be called more than once before DOM is loaded }; document.loaded ? callback() : document.observe(''dom:loaded'', callback); } }); TAG On Apr 14, 2008, at 4:03 PM, jdalton wrote:> > > > var callback = function(){ > //do stuff > }; > > if(document.loaded) callback(); > else document.observe(''dom:loaded'', callback); > > - JDD > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Stillwell
2008-Apr-14 22:36 UTC
Re: evaluate when XHR request completes *and* dom:loaded?
On Apr 14, 11:15 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> More to your situation, just plug jdalton''s code inside your > onSuccess declaration: > > var ajax = new Ajax.Request(url, { > onSuccess: function(res) { > // jdalton''s code: > var callback = function() { > // Do stuff; can use res as an in-scope variable. > // Be careful if this callback could be called more than > once before DOM is loaded > }; > > document.loaded ? callback() : document.observe(''dom:loaded'', > callback); > } > });Thanks. I considered this, but thought I might get hit by race conditions between the document.loaded check and document.observe() setting the event up. It''s not possible for document.loaded to be set between the two? --M. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
In Prototype 1.6+ there is a bug that has the dom:loaded observers fire before it sets the document.loaded prop, but thats the only issue with the flag that i know of: http://dev.rubyonrails.org/ticket/11453 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Here is a patch for the document.observe(''dom:loaded'') observers to fire instantly if the document has already loaded: http://dev.rubyonrails.org/attachment/ticket/11595/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry for the spam: i typoed the link: http://dev.rubyonrails.org/ticket/11595 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---