I have a simple eRb snippet that get''s a hash via an objects method call, and populates a Javascript call... for example: <head> <script src="http://www.asdf.com/asdfasfd.js"></script> </head> <body> <%= @hash = asdf.getHash() %> <script> blah.doSomething(<%= @hash[''blah''] %>); </script> </body> 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 ? Any insight into this would be appreciated ! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Dylan Stamat wrote:> <body> > <%= @hash = asdf.getHash() %> > <script> > blah.doSomething(<%= @hash[''blah''] %>); > </script> > </body> > > 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.Should the hash value be quoted? blah.doSomething( ''<%= @hash[''blah''] -%>'' ); -- We develop, watch us RoR, in numbers too big to ignore.
I''m actually extracting the values from the hash above the script... so: # Get the Hash from the Method <% @location_hash = @listing.getData %> # Assign "@stuff" the value of the stuff value in the Hash <% @stuff = @location_hash[''stuff''] %> # Use "@stuff" in the Javascript new RandomJavascriptObject(<%= @stuff %>); 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. On 11/10/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Dylan Stamat wrote: > > > <body> > > <%= @hash = asdf.getHash() %> > > <script> > > blah.doSomething(<%= @hash[''blah''] %>); > > </script> > > </body> > > > > 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. > > Should the hash value be quoted? > > blah.doSomething( ''<%= @hash[''blah''] -%>'' ); > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > 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
Dylan Stamat wrote:> # Use "@stuff" in the Javascript > new RandomJavascriptObject(<%= @stuff %>); > > 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? -- We develop, watch us RoR, in numbers too big to ignore.
The parameter for the Javascript Object is a number. I tried doing @ stuff.to_i and @stuff.to_s, but no luck. On 11/10/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Dylan Stamat wrote: > > > # Use "@stuff" in the Javascript > > new RandomJavascriptObject(<%= @stuff %>); > > > > 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? > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > 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
Dylan Stamat wrote:> The parameter for the Javascript Object is a number. I tried doing > @stuff.to_i and @stuff.to_s, but no luck.Hmm, all I can think of trying is to change the closing tag from "%>" to "-%>" to keep the closing parenthesis of the function call on the same line.
Hmm.... no luck. I''m baffled :) 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 ! =Dylan On 11/11/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Dylan Stamat wrote: > > The parameter for the Javascript Object is a number. I tried doing > > @stuff.to_i and @stuff.to_s, but no luck. > > Hmm, all I can think of trying is to change the closing tag > from "%>" to "-%>" to keep the closing parenthesis of the > function call on the same line. > > _______________________________________________ > 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
I just tried doing a custom "render", and also put the Javascript call with hardcoded values in a file to "render_partial"... but no luck. WTF. On 11/11/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hmm.... no luck. I''m baffled :) > > 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 ! > => Dylan > > > On 11/11/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > > Dylan Stamat wrote: > > > The parameter for the Javascript Object is a number. I tried doing > > > @stuff.to_i and @stuff.to_s, but no luck. > > > > Hmm, all I can think of trying is to change the closing tag > > from "%>" to "-%>" to keep the closing parenthesis of the > > function call on the same line. > > > > _______________________________________________ > > 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
Anybody have any ideas ? Just trying to pass a parameter into a Javascript method. Simple stuff, but it seems impossible ! On 11/11/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I just tried doing a custom "render", and also put the Javascript call > with hardcoded values in a file to "render_partial"... but no luck. > WTF. > > > On 11/11/05, Dylan Stamat <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hmm.... no luck. I''m baffled :) > > > > 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 ! > > => > Dylan > > > > > > On 11/11/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org > wrote: > > > > > > Dylan Stamat wrote: > > > > The parameter for the Javascript Object is a number. I tried doing > > > > @stuff.to_i and @stuff.to_s, but no luck. > > > > > > Hmm, all I can think of trying is to change the closing tag > > > from "%>" to "-%>" to keep the closing parenthesis of the > > > function call on the same line. > > > > > > _______________________________________________ > > > 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
it will work if you wait until the page has loaded before calling the javascript function: <body onload="myjavascriptfunction(''<%= @some_msg %>'')"> 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. -lv Dylan Stamat wrote:> Anybody have any ideas ? Just trying to pass a parameter into a > Javascript method. Simple stuff, but it seems impossible ! > > > On 11/11/05, *Dylan Stamat* <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > I just tried doing a custom "render", and also put the Javascript > call with hardcoded values in a file to "render_partial"... but no luck. > WTF. > > > > On 11/11/05, *Dylan Stamat* <dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <mailto:dylans-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> wrote: > > Hmm.... no luck. I''m baffled :) > > 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 ! > => Dylan > > > > On 11/11/05, *Mark Reginald James* > <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org > <mailto:mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org>> wrote: > > Dylan Stamat wrote: >> The parameter for the Javascript Object is a number. I > tried doing >> @stuff.to_i and @stuff.to_s, but no luck. > > Hmm, all I can think of trying is to change the closing tag > from "%>" to "-%>" to keep the closing parenthesis of the > function call on the same line. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > <mailto: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
Dylan Stamat wrote:> 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: <%= javascript_tag "new RandomJavascriptObject( #{@stuff} );" -%> -- We develop, watch us RoR, in numbers too big to ignore.