I have Ajax working fine to return html code and display it in a div. Now, I would like to return some JavaScript generated in the controller, and have this execute in the browser. I tried assigning the script code to a div, which successfully put the script into the div, but the code is never executed. What I would like is something like DWR (http://getahead.ltd.uk/dwr/) that allows you to define a JS function that will be executed with the returned ajaxed data. Is this possible in Rails? I can port over the DWR code, but I don''t want to reinvent the wheel. Thanks! --Dave.
The only solution I''ve found for this is to have: - a div with javascript in it (hidden with css) - an image with an onload="eval($(''somediv'').innerHTML)" attribute (hidden with css) The on* events trigger, but inline javascript doesn''t. Your javascript in the div can define function as well as execute them, just like stuff in a script tag. Of course, you could always define the script in the enclosing page. :-) Oh, and make sure the image you''re referencing exists. Otherwise the onload won''t run. Steve On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote:> > I have Ajax working fine to return html code and display it in a div. > Now, I would like to return some JavaScript generated in the controller, > and have this execute in the browser. I tried assigning the script code > to a div, which successfully put the script into the div, but the code > is never executed. > > What I would like is something like DWR (http://getahead.ltd.uk/dwr/) > that allows you to define a JS function that will be executed with the > returned ajaxed data. Is this possible in Rails? I can port over the > DWR code, but I don''t want to reinvent the wheel. > > Thanks! > --Dave. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Jun 1, 2005, at 9:07 PM, David Teare wrote:> > I have Ajax working fine to return html code and display it in a > div. Now, I would like to return some JavaScript generated in the > controller, and have this execute in the browser. I tried > assigning the script code to a div, which successfully put the > script into the div, but the code is never executed. > > What I would like is something like DWR (http://getahead.ltd.uk/ > dwr/) that allows you to define a JS function that will be executed > with the returned ajaxed data. Is this possible in Rails? I can > port over the DWR code, but I don''t want to reinvent the wheel.I have fun learning things by attempting to answer. I quickly hacked this together: View: <input type="button" onclick="test(''client'')"/> <script> function test(val) { var opts = { parameters: "param=" + val, onComplete: function(data) { eval(data.responseText); } }; var req = new Ajax.Request(''/controller/test'', opts); } </script> Controller: def test render_text "alert(''#{@params[:param]} -> server'');" end Don''t take this as _the_ way to do it, as I''m new to Rails and the mystical Prototype JavaScript additions, but this worked beautifully on my first try. I got a pop-up on my browser when I clicked the button that said "client -> server". I''d love to see what others have to say on this topic though. Questions - why send JavaScript down? What advantage is there to doing? I can envision it being useful to update multiple elements at one time perhaps, but I''m curious what your use case is. Erik
Thanks Erik, that looks exactly like what I wanted to do. The use case I was planning on was exactly what you suspected - I wanted to update multiple items on the screen from a single ajax request. The other thing I have been interested in for a while is being able to have my own JS code handle the result from the ajax request and display it (or store it), instead of just always putting it into a div. I never saw Ajax.Request used directly before, but now I see how easy it is to have full control over the response (but likely 90% of the time the convience method Ajax.Updater will be used). Thanks! Erik Hatcher wrote:> > On Jun 1, 2005, at 9:07 PM, David Teare wrote: > >> >> I have Ajax working fine to return html code and display it in a >> div. Now, I would like to return some JavaScript generated in the >> controller, and have this execute in the browser. I tried assigning >> the script code to a div, which successfully put the script into the >> div, but the code is never executed. >> >> What I would like is something like DWR (http://getahead.ltd.uk/ >> dwr/) that allows you to define a JS function that will be executed >> with the returned ajaxed data. Is this possible in Rails? I can >> port over the DWR code, but I don''t want to reinvent the wheel. > > > I have fun learning things by attempting to answer. I quickly hacked > this together: > > View: > <input type="button" onclick="test(''client'')"/> > <script> > function test(val) { > var opts = { > parameters: "param=" + val, > onComplete: function(data) { > eval(data.responseText); > } > }; > var req = new Ajax.Request(''/controller/test'', opts); > } > </script> > > Controller: > def test > render_text "alert(''#{@params[:param]} -> server'');" > end > > Don''t take this as _the_ way to do it, as I''m new to Rails and the > mystical Prototype JavaScript additions, but this worked beautifully > on my first try. I got a pop-up on my browser when I clicked the > button that said "client -> server". I''d love to see what others > have to say on this topic though. > > Questions - why send JavaScript down? What advantage is there to > doing? I can envision it being useful to update multiple elements at > one time perhaps, but I''m curious what your use case is. > > Erik > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Wow - that looks complicated! I would have never thought of that. Erik''s approach looks much more straight forward. Steve Willer wrote: The only solution I''ve found for this is to have: - a div with javascript in it (hidden with css) - an image with an onload="eval($(''somediv'').innerHTML)" attribute (hidden with css) The on* events trigger, but inline javascript doesn''t. Your javascript in the div can define function as well as execute them, just like stuff in a script tag. Of course, you could always define the script in the enclosing page. :-) Oh, and make sure the image you''re referencing exists. Otherwise the onload won''t run. Steve On 6/1/05, David Teare wrote: I have Ajax working fine to return html code and display it in a div. Now, I would like to return some JavaScript generated in the controller, and have this execute in the browser. I tried assigning the script code to a div, which successfully put the script into the div, but the code is never executed. What I would like is something like DWR (http://getahead.ltd.uk/dwr/) that allows you to define a JS function that will be executed with the returned ajaxed data. Is this possible in Rails? I can port over the DWR code, but I don''t want to reinvent the wheel. Thanks! --Dave. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Erik Hatcher wrote:> Questions - why send JavaScript down? What advantage is there to > doing? I can envision it being useful to update multiple elements at > one time perhaps, but I''m curious what your use case is.I was previously working on this in patch 933: http://dev.rubyonrails.com/ticket/933#preview Indeed, I use this to update multiple locations at the same time, and to update several form values. This helper is used in a view or partial, the javascript content of which is returned to the client, where it is eval''d - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCnpcLSIxC85HZHLMRAmLkAJ9ZqI339BVXK9uYD9C0wzM5ZleAyACeLO9d 1PXgO1YTdv/aw2kib0q+bQs=E8Q7 -----END PGP SIGNATURE-----
Actually, I think both are. Executing script that is returned by a view is as simple as using a :complete => ''eval(request.responseText'' insted of the :update => ''...'' in the call to link_to_remote. That''s all there is to it. There is also an example of this in my chapter of the beta rails book. btw, the current version of the Upload Progress Patch updates Prototype to have an optional feature to have <script></script> blocks that are returned with Ajax to be executed. This way, you can just return normal HTML intermixed with Javascripts. See here: http://dev.rubyonrails.com/attachment/ticket/1026/ upload_progress_1356-3.diff (search for "prototype.js"). --- Thomas Am 02.06.2005 um 04:02 schrieb David Teare:> Wow - that looks complicated! I would have never thought of that. > > Erik''s approach looks much more straight forward.
On Jun 2, 2005, at 2:02 AM, Thomas Fuchs wrote:> Actually, I think both are. > > Executing script that is returned by a view is as simple as using a > :complete => ''eval(request.responseText'' insted of the :update => > ''...'' in the > call to link_to_remote. That''s all there is to it.Ah yes! In fact, I''m using that feature in another part of my application so I should have remembered and mentioned that. I haven''t looked, but I presume there is a similar helper to generate a button instead of a hyperlink?> There is also an example of this in my chapter of the beta rails book.Indeed there is - and everyone on this list should own that book :) Thomas - thank you for your wonderful work with Prototype, the effects, and AJAXy stuff. Great work! Erik
The problem with Erik''s approach is that it requires user action. What if you want to unhide a div when an ajax''d page loads? I was told today, btw, that IFRAME also has an onload feature, even without a SRC attribute. So potentially you could stick your javascript in the iframe and put an onload that''s something like onload="eval(this.innerHTML)". That''s very nearly script tag-like. I haven''t tried it yet, though. On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote:> Wow - that looks complicated! I would have never thought of that. > > Erik''s approach looks much more straight forward. > > > Steve Willer wrote: > The only solution I''ve found for this is to have: > > - a div with javascript in it (hidden with css) > - an image with an onload="eval($(''somediv'').innerHTML)" > attribute > (hidden with css) > > The on* events trigger, but inline javascript doesn''t. Your javascript > in the div can define function as well as execute them, just like > stuff in a script tag. > > Of course, you could always define the script in the enclosing page. :-) > > Oh, and make sure the image you''re referencing exists. Otherwise the > onload won''t run. > > > Steve > > > On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: > > > I have Ajax working fine to return html code and display it in a div. > Now, I would like to return some JavaScript generated in the controller, > and have this execute in the browser. I tried assigning the script code > to a div, which successfully put the script into the div, but the code > is never executed. > > What I would like is something like DWR (http://getahead.ltd.uk/dwr/) > that allows you to define a JS function that will be executed with the > returned ajaxed data. Is this possible in Rails? I can port over the > DWR code, but I don''t want to reinvent the wheel. > > Thanks! > --Dave. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
On Jun 2, 2005, at 5:00 PM, Steve Willer wrote:> The problem with Erik''s approach is that it requires user action. What > if you want to unhide a div when an ajax''d page loads?I''m not following what you''re saying is the difference here. In my approach (which is just a more verbose way of doing what Thomas showed with the link_to_remote technique), if I want something to happen when the page loads I put it in the onComplete handler (or :complete mapping with link_to_remote). In onComplete you could put Element.hide(''div''). How does your approach do something different/better? Erik> > I was told today, btw, that IFRAME also has an onload feature, even > without a SRC attribute. So potentially you could stick your > javascript in the iframe and put an onload that''s something like > onload="eval(this.innerHTML)". That''s very nearly script tag-like. I > haven''t tried it yet, though. > > > On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: > >> Wow - that looks complicated! I would have never thought of that. >> >> Erik''s approach looks much more straight forward. >> >> >> Steve Willer wrote: >> The only solution I''ve found for this is to have: >> >> - a div with javascript in it (hidden with css) >> - an image with an onload="eval($(''somediv'').innerHTML)" >> attribute >> (hidden with css) >> >> The on* events trigger, but inline javascript doesn''t. Your >> javascript >> in the div can define function as well as execute them, just like >> stuff in a script tag. >> >> Of course, you could always define the script in the enclosing >> page. :-) >> >> Oh, and make sure the image you''re referencing exists. Otherwise the >> onload won''t run. >> >> >> Steve >> >> >> On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: >> >> >> I have Ajax working fine to return html code and display it in a >> div. >> Now, I would like to return some JavaScript generated in the >> controller, >> and have this execute in the browser. I tried assigning the script >> code >> to a div, which successfully put the script into the div, but the >> code >> is never executed. >> >> What I would like is something like DWR (http://getahead.ltd.uk/dwr/) >> that allows you to define a JS function that will be executed with >> the >> returned ajaxed data. Is this possible in Rails? I can port over the >> DWR code, but I don''t want to reinvent the wheel. >> >> Thanks! >> --Dave. >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Oh, I guess I just prefer to keep actions that relate to the content in with the content. What I mean is that the Ajax''d link (in the parent page) doesn''t necessarily know the structure or content of the contained page. There may be unique id''s in there. An example might be if the contained page was a tree that needs to be autoexpanded with javascript, and the parent page doesn''t know which. In those kinds of cases, I would need to have some javascript defined somewhere in the contained page that at least defines a variable indicating which nodes to autoexpand. If I''m going to have that bit of javascript in (and have it execute), I might as well have the command to autoexpand itself, and the script that does the autoexpanding, to keep all of the javascript logic for that tree in with the tree itself instead of with the parent page. You could have an oncomplete action that calls a function in the parent page, but the contained page would at least need to define which nodes to expand in this case. Your example may be the 80% case, where you know the name of the div or divs you want to expand. I don''t think it''s the 100% case, though ... and really, the iframe approach is quite simple and would allow you the ability to keep javascript in with the html it manipulates. It would also work without changes if you want the features to work without Ajax. Steve On 6/2/05, Erik Hatcher <erik-LIifS8st6VgJvtFkdXX2HpqQE7yCjDx5@public.gmane.org> wrote:> > On Jun 2, 2005, at 5:00 PM, Steve Willer wrote: > > The problem with Erik''s approach is that it requires user action. What > > if you want to unhide a div when an ajax''d page loads? > > I''m not following what you''re saying is the difference here. > > In my approach (which is just a more verbose way of doing what Thomas > showed with the link_to_remote technique), if I want something to > happen when the page loads I put it in the onComplete handler > (or :complete mapping with link_to_remote). In onComplete you could > put Element.hide(''div''). > > How does your approach do something different/better? > > Erik > > > > > > I was told today, btw, that IFRAME also has an onload feature, even > > without a SRC attribute. So potentially you could stick your > > javascript in the iframe and put an onload that''s something like > > onload="eval(this.innerHTML)". That''s very nearly script tag-like. I > > haven''t tried it yet, though. > > > > > > On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: > > > >> Wow - that looks complicated! I would have never thought of that. > >> > >> Erik''s approach looks much more straight forward. > >> > >> > >> Steve Willer wrote: > >> The only solution I''ve found for this is to have: > >> > >> - a div with javascript in it (hidden with css) > >> - an image with an onload="eval($(''somediv'').innerHTML)" > >> attribute > >> (hidden with css) > >> > >> The on* events trigger, but inline javascript doesn''t. Your > >> javascript > >> in the div can define function as well as execute them, just like > >> stuff in a script tag. > >> > >> Of course, you could always define the script in the enclosing > >> page. :-) > >> > >> Oh, and make sure the image you''re referencing exists. Otherwise the > >> onload won''t run. > >> > >> > >> Steve > >> > >> > >> On 6/1/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote: > >> > >> > >> I have Ajax working fine to return html code and display it in a > >> div. > >> Now, I would like to return some JavaScript generated in the > >> controller, > >> and have this execute in the browser. I tried assigning the script > >> code > >> to a div, which successfully put the script into the div, but the > >> code > >> is never executed. > >> > >> What I would like is something like DWR (http://getahead.ltd.uk/dwr/) > >> that allows you to define a JS function that will be executed with > >> the > >> returned ajaxed data. Is this possible in Rails? I can port over the > >> DWR code, but I don''t want to reinvent the wheel. > >> > >> Thanks! > >> --Dave. > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > >> > >> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >