I have a simple eRb snippet that get’s a hash via an objects method
call,
and populates a Javascript call… for example:
<%= @hash = asdf.getHash() %>
When I view the source of the resulting HTML, the doSomething() method
is
indeed populated with the correct value from the hash… however, the
actual
method get’s called “without” the value.
So, I’m assuming that there is some reason the Javascript is being
rendered
and executed prior to the eRb code.
Is this correct ?
When I view the source of the resulting HTML, the doSomething() method
is indeed populated with the correct value from the hash… however, the
actual method get’s called “without” the value.
The catch is, if I hardcode the value of @stuff in the actual HTML, and
load
the page… everything works fine.
If I use eRb to do the printing like above, the value is actually
printed in
the HTML, but the actual value passed to the Javascript is empty.
The catch is, if I hardcode the value of @stuff in the actual HTML, and
load the page… everything works fine.
If I use eRb to do the printing like above, the value is actually
printed in the HTML, but the actual value passed to the Javascript is empty.
What type of JavaScript object is the parameter? Number, string, or
other?
The HTML source has the function and the parameters on the same line:
new BlahObject(1234, 1234);
… and if I hardcode the parameters to replicate the above, everything
works out perfectly.
So, I’d have to assume that this is a “Javascript loading before eRb”
issue… or something similar.
I’ll keep trying different options to see if I can’t nail this down.
Thanks for you help thus far !
Anybody have any ideas ? Just trying to pass a parameter into a
Javascript method. Simple stuff, but it seems impossible !
Perhaps your web server is delivering the function name
and parameters in different response chunks because of
possible long delays in erb output, and your web browser
is not waiting for all the chunks before evaluating the
script section. Try generating the whole script section in erb:
it will work if you wait until the page has loaded before calling the
javascript function:
it works for me, but not, as you have pointed out, when you try to call
javascript
before the page has finished loading.
script/about
About your application’s environment
Ruby version 1.8.3 (i386-cygwin)
RubyGems version 0.8.11
Rails version 0.14.3
Active Record version 1.13.0
Action Pack version 1.11.0
Action Web Service version 0.9.3
Action Mailer version 1.1.3
Active Support version 1.2.3
I have noticed that for some reason beyond my understanding javascript
functions work
more consistently when placed either in the HEAD or “included” within
the HEAD (not w/in the BODY).
And some of the most common javascript errors are caused by race
conditions (e.g. page loading),
which is why I always wait for the page to load before calling any
javascript.