Michael Sharman
2008-Jan-23 05:13 UTC
Event.observe() throwing error when element not on page
Hi guys, In my DOM ready login (Event.observe(window, ''load'', function() etc...) I am setting up my page observers. This is working great except certain elements aren''t available on all pages so their causing Javascript errors when the observer is looking for them and they aren''t there. Currently I''m getting around this by doing the following in my DOM ready script: $(''frm'') === null ? "" : Event.observe(''frm'', ''submit'', handler); Now while this works I''m not sure if it follows "best practise" for Prototype. Thoughts? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Jan-23 05:49 UTC
Re: Event.observe() throwing error when element not on page
On Jan 22, 2008 11:13 PM, Michael Sharman <sharmo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is working great > except certain elements aren''t available on all pages so their causingYou should not be attempting to attach events to elements that are not on the page, Prototype is not a "fail silently" framework. I usually do something like your above code, except I would fetch the element once to avoid multiple $() calls, such as: var elem = $(''frm''); if (elem) elem.observe(''submit'', handler); -justin --~--~---------~--~----~------------~-------~--~----~ 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 Sharman
2008-Jan-23 10:15 UTC
Re: Event.observe() throwing error when element not on page
Hi Justin,>You should not be attempting to attach events to elements that are not on the pageThe code is in an external .js file and there are only a couple of pages where that element doesn''t appear.>var elem = $(''frm'');I guess I can''t do that though as $(''frm'') won''t exist and throw an error. Remember that this code exists in the ''onload'' code block. On Jan 23, 4:49 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 22, 2008 11:13 PM, Michael Sharman <sha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This is working great > > except certain elements aren''t available on all pages so their causing > > You should not be attempting to attach events to elements that are not > on the page, Prototype is not a "fail silently" framework. > > I usually do something like your above code, except I would fetch the > element once to avoid multiple $() calls, such as: > > var elem = $(''frm''); > if (elem) elem.observe(''submit'', handler); > > -justin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ken Snyder
2008-Jan-23 15:16 UTC
Re: Event.observe() throwing error when element not on page
Michael Sharman wrote:> Hi Justin, > > >> You should not be attempting to attach events to elements that are not on the page >> > > The code is in an external .js file and there are only a couple of > pages where that element doesn''t appear. > > >> var elem = $(''frm''); >> > > I guess I can''t do that though as $(''frm'') won''t exist and throw an > error. Remember that this code exists in the ''onload'' code block. >Why CAN''T you do it? If you need to keep the error console clean, use a try-catch block. try { $(''frm'').observe(''submit'', handler); } catch (e) {} - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
arschles-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-23 17:13 UTC
Re: Event.observe() throwing error when element not on page
I may misunderstand your question, but you can always wait until the window loads: Event.observe(window, ''load'', function() { Event.observe(''frm'', ''submit'', handler); } ); On Jan 23, 10:16 am, Ken Snyder <kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael Sharman wrote: > > Hi Justin, > > >> You should not be attempting to attach events to elements that are not on the page > > > The code is in an external .js file and there are only a couple of > > pages where that element doesn''t appear. > > >> var elem = $(''frm''); > > > I guess I can''t do that though as $(''frm'') won''t exist and throw an > > error. Remember that this code exists in the ''onload'' code block. > > Why CAN''T you do it? If you need to keep the error console clean, use a > try-catch block. > > try { $(''frm'').observe(''submit'', handler); } catch (e) {} > > - Ken Snyder--~--~---------~--~----~------------~-------~--~----~ 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, "var elem = $(''frm'');" will not throw error if element with id of "frm" does not exist. It will assign null to "elem" variable. Justin''s example will work as expected and is one of the preferred ways to check for element existence. Alternatively, Element.observe(''frm'', ''submit'', handler) will fail *silently* if element is not present. I would avoid using "try ... catch" as it happens to be quite expensive (speed wise) Hope this helps. Best, kangax --~--~---------~--~----~------------~-------~--~----~ 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 Sharman
2008-Jan-23 21:42 UTC
Re: Event.observe() throwing error when element not on page
Hi kangax (and Justin), Thanks for that, makes sense now I have morning clarity! Appreciate your time :) On Jan 24, 7:51 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael, > > "var elem = $(''frm'');" will not throw error if element with id of > "frm" does not exist. It will assign null to "elem" variable. > > Justin''s example will work as expected and is one of the preferred > ways to check for element existence. > > Alternatively, Element.observe(''frm'', ''submit'', handler) will fail > *silently* if element is not present. > > I would avoid using "try ... catch" as it happens to be quite > expensive (speed wise) > > Hope this helps. > > Best, > kangax--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---