# Triggering Javascript from a Ruby form

**URL:** <https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407>\
**Category:** Rails\
**Created:** [September 4, 2010, 2:14am UTC](https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407 "2010-09-04T02:14:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marsdragon](https://www.ruby-forum.com/u/marsdragon)\
**Post date:** [September 4, 2010, 2:14am UTC](https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407/1 "2010-09-04T02:14:39Z")

</div>

Hello again,

I’ve managed to hack together an uploader, but I need a progress bar to  
go with it. I ended up using FTP to upload, and got a way to find the  
progress while it’s uploading. I split that off into another function in  
the controller, which is all well and good.

The problem is that I need to repeatedly call the how\_finished function  
while the file is uploading. The form goes to the uploading function, so  
I need another way to repeatedly call how\_finished. The best way seems  
to be with Javascript and AJAX, but I can’t find a good way of setting  
up and calling a Javascript function to get the information from the  
controller. Looking at other Ruby progress bars  
([http://railsillustrated.com/screencast-file-uploads-progress-in-rails-passenger.html](http://railsillustrated.com/screencast-file-uploads-progress-in-rails-passenger.html))  
it seems to be possible to call the controller with Javascript, but that  
also requires a separate module for Apache that doesn’t work for FTP  
uploads. I’ve been looking around at JSON, but I don’t really understand  
it or how to use it to do what I want.

I’m pretty sure I have all the information I need in the program, the  
problem is getting it all together and I’m not sure if that’s even  
possible. Does anyone have ideas?

---

<div class="post-metadata">

**Author:** ![marsdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marsdragon](https://www.ruby-forum.com/u/marsdragon)\
**Post date:** [September 5, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407/2 "2010-09-05T21:12:25Z")

</div>

A. Leek wrote:

> I’m pretty sure I have all the information I need in the program, the  
> problem is getting it all together and I’m not sure if that’s even  
> possible. Does anyone have ideas?

It may be too narrowly supported for what your trying, but you can go to  
the HTML5 route. Check out the File API:  
[File API](http://dev.w3.org/2006/webapi/FileAPI/), which shows the new javascript  
events that get fired when you do an upload. You can tie into the  
following and perform actions all on the client side:

onloadstart  
onprogress  
onabort  
onerror  
onload  
onloadend

If you need a more legacy (well, meaning it will support IE) solution  
you can look at SWFUpload. It has similar events, but uses Flash and  
Javascript to handle files and events.

> **[Playbet88 : Daftar Agen Live Casino Online Uang Asli Terpercaya\<](https://swfupload.org/)**
>
> Playbet88 merupakan agen situs judi online terpercaya indonesia yang menyediakan banyak permainan gampang menang setiap saat.

---

<div class="post-metadata">

**Author:** ![marsdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marsdragon](https://www.ruby-forum.com/u/marsdragon)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407/3 "2010-09-06T16:39:36Z")

</div>

I think one of the things you might need is a ‘timer’, which will let  
your code ‘sleep’ before calling again your ‘how\_finished’ function. I  
have never done this but remember reading about it, I googled for  
‘javascript sleep timer’ and one of the links I found was this:

> **[Is there a sleep/wait javascript function for firefox?](https://www.daniweb.com/programming/web-development/threads/47199/is-there-a-sleep-wait-javascript-function-for-firefox)**
>
> I need to suspend processing in my script for a few seconds, and i am not sure how. Thanks, sj

In order to communicate with the server from Javascript I would look  
into Prototype or jQuery. They simplify enormously the code in  
Javascript to be able to communicate with your server using AJAX.

I hope this gets you started.

---

<div class="post-metadata">

**Author:** ![marsdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marsdragon](https://www.ruby-forum.com/u/marsdragon)\
**Post date:** [September 6, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/triggering-javascript-from-a-ruby-form/194407/4 "2010-09-06T17:09:11Z")

</div>

pepe wrote:

> I think one of the things you might need is a ‘timer’, which will let  
> your code ‘sleep’ before calling again your ‘how\_finished’ function. I  
> have never done this but remember reading about it, I googled for  
> ‘javascript sleep timer’ and one of the links I found was this:  
> [Is there a sleep/wait javascript function ... | DaniWeb](http://www.daniweb.com/forums/thread47199.html)

Be it that he is using Rails he is certainly using Prototype, or perhaps  
jQuery, already.

I have to disagree with the use of a timer (setInterval) in this  
context, considering the hugely evented nature of Javascript and what  
already exists out there.
