Re: Returning JS in AJAX

Steve Willer
2005-06-03T15:27:46Z

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
> >
>
>