# 'document.observe' and a variable generated in controller

**URL:** <https://www.ruby-forum.com/t/document-observe-and-a-variable-generated-in-controller/172445>\
**Category:** Rails\
**Created:** [July 21, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/document-observe-and-a-variable-generated-in-controller/172445 "2009-07-21T16:57:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [July 21, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/document-observe-and-a-variable-generated-in-controller/172445/1 "2009-07-21T16:57:40Z")

</div>

I have a java script function which I want to call once the page has  
loaded.

Initially it looked like this:

I was then advised by people on this forum that it is good practice to  
put this into an external js file thus:

document.observe(‘dom:loaded’, function() {  
doStuff();  
});

That also worked fine.

Now, I want to pass a variable to this function which is generated in my  
application’s controller (@member)

If I do this in my view, everything works fine:

Yet if I do this in the js file, then nothing works (as might be  
expected).

document.observe(‘dom:loaded’, function() {  
doStuff(@member);  
});

Is there any way to use ‘document.observe’ in an external js file with a  
variable generated in my ruby controller?  
If not, what is the best practice in this case?

Thanks in advance
