So prototype provides Element.remove(). In an html file I also have a function named remove(). Why do these clash when I do the following: <input type="button" onClick="remove()" value="Delete" /> Instead of calling the remove function I defined, it actually removes the delete button. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Prototype extends HTML Elements with functions that proxy to the Element class. So "remove()" is really calling "this.remove()", which gets proxied to "Element.remove(this)", which removes the <input> element. I would recommend renaming the remove function. On Oct 20, 2006, at 3:26 PM, Michael Peters wrote:> > So prototype provides Element.remove(). In an html file I also have > a function > named remove(). Why do these clash when I do the following: > > <input type="button" onClick="remove()" value="Delete" /> > > Instead of calling the remove function I defined, it actually > removes the delete > button. > > -- > Michael Peters > Developer > Plus Three, LP > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Interesting. I guess it has to do with order of operations. It''s first looking for the remove() function as a member of itself (the element), which it finds because of the prototype extension (prototype''s Element functions must now be applied automatically to all new elements... I''m still on an old version due to lack of time for upgrading, but I think that''s the case in the latest release) So basically it''s equating to "this.remove()". This is why creating global functions on a page has gone the way of the dinosaurs (namespace collisions, or also called "polluting the global namespace"). I recommend that you consider authoring all your home grown functions inside your own namespace object to avoid these problems (with prototype or any other library you end up using)... var MPeters = {}; MPeters.remove = function() {...} <input type="button" onClick="MPeters.remove()" value="Delete" /> On 10/20/06, Michael Peters <mpeters-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> > > So prototype provides Element.remove(). In an html file I also have a > function > named remove(). Why do these clash when I do the following: > > <input type="button" onClick="remove()" value="Delete" /> > > Instead of calling the remove function I defined, it actually removes the > delete > button. > > -- > Michael Peters > Developer > Plus Three, LP > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Gahl wrote:> So basically it''s equating to "this.remove()".That''s what we thought too, but for some reason I didn''t think those Element.* methods were added to each element.> This is why creating global functions on a page has gone the way of the > dinosaurs (namespace collisions, or also called "polluting the global > namespace").I also thought Prototype had fixed all of these issues.> I recommend that you consider authoring all your home grown functions > inside your own namespace object to avoid these problems (with prototype > or any other library you end up using)...Yeah, we temporarily renamed the offending sub and we''ll be making project specific namespaces for them later on. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Ryan Gahl wrote: > >> So basically it''s equating to "this.remove()". > > That''s what we thought too, but for some reason I didn''t think > those Element.* > methods were added to each element.I believe this is new for Prototype 1.5.>> This is why creating global functions on a page has gone the way >> of the >> dinosaurs (namespace collisions, or also called "polluting the global >> namespace"). > > I also thought Prototype had fixed all of these issues.Prototype doesn''t pollute the global namespace. It only extends HTML Element objects. If you call "remove()" from anywhere other than in the context of that element (such as from a <script> block), you will not get that function. It only appears to pollute the global namespace because JS automatically tries "this" as the first receiver for the "remove" method, because you are calling it from the onclick handler.> >> I recommend that you consider authoring all your home grown functions >> inside your own namespace object to avoid these problems (with >> prototype >> or any other library you end up using)... > > Yeah, we temporarily renamed the offending sub and we''ll be making > project > specific namespaces for them later on.This is really the way to go for anything even moderately complicated. Brad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > I also thought Prototype had fixed all of these issues.This isn''t necessarily Prototype''s fault. Well it is and it isn''t. It is because yes, it extends the behaviors of the elements which resulted in your little collision. But it isn''t because it''s this type of "sugar" that people who use it are looking for. It''s really a good practice to get into anyway, to create namespaces instead of global functions. So unfortunately you ran into it by mistake, but something good came of it, in that you now realize what to do to avoid it in the future, and again, not just in regards to Prototype but any lib you drop in for whatever reason... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---