# Whatever happend to unobtrusive javascript in Rails?

**URL:** <https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094>\
**Category:** Rails\
**Created:** [March 5, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094 "2006-03-05T22:53:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 5, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/1 "2006-03-05T22:53:19Z")

</div>

Don’t get me wrong, JavaScript/Ajax helpers in Rails are a huge  
timesaver and they have helped me to finally overcome my irrational  
aversion to js libraries like prototype and such. However, all this  
goodness seems to come at quiet a price. The resulting code is  
littered with inline JS, including ubiquitous script tags and onload  
attributes etc… It seems that just when i found myself fully  
appreciating the advantages of graceful degradation and progressive  
enhancement and gotten myself into a habit of writing clean,  
unobtrusive dom scripts – fully separated from the presentation  
layer – i’m now again faced with with a dilemma in Rails: Here is a  
framework that is all about beautiful code and strict separation, yet  
i suppose to just throw out all good intentions on the client side?

I also don’t think the above concepts are superficial, contrived or  
inappropriately dogmatic. There are tremendous real-life benefits and  
in some cases they even become necessities. There’s also a more  
metaphysical aspect - if you will - the immense satisfaction of  
looking at squeaky clean xhtml source, yet “invisibly” enhanced by  
advanced DOM-scripting functionality and AJAX glitz. I understand  
that i’m not required to use JS helpers in Rails but i feel i would  
miss out on a great deal of very productive features if i decided to  
implement all this manually for the sake of progressive enhancement.

Also, it strikes me as something that could prolly be fixed rather  
easily (ok, i’m being a little presumptuous), since it’s not a  
limitation of Prototype etc, but the way JS helpers are implemented  
in Rails. I heard a lot about RJS templates (i’m not using edge yet),  
but as far as i can tell - while prolly tremendously helpful - they  
seem to do nothing to overcome this particular problem.

So, my question, how have other people solved this problem? Is there  
any kind of development (Core or otherwise) that will help to  
eliminate inline JS yet keep within the friendly confines of JS/Ajax  
helpers in Rails? Or is there simply not much interest within the  
Rails community?

I apologize if this has been brought up before, but unfortunately the  
mailing-list archive search is still not working and I wasn’t able to  
find any relevant discussion searching on google. If this is all an  
old hat, feel free to direct me to the appropriate discussion. I’m  
brand new to RoR and i can only imagine that this must have come up  
many times before, so i dont want to bore anybody with questions that  
have been discussed ad nauseam.

Thanks.  
Sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 6, 2006, 12:24am UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/2 "2006-03-06T00:24:08Z")

</div>

Hi,

I’m about a few days of releasing a plugin which will solve your  
problems and add a load of extra functionality to the rails javascript  
helpers generally. As a bit of a sneak peek you can view the rdocs at  
[http://www.vivabit.com/unobtrusivedocs/](http://www.vivabit.com/unobtrusivedocs/) Don’t try to install it yet  
though because it’s not production ready yet. However, it when it is  
I’ll post it on this list. I’m glad that there are other people are  
starting to become interested in doing javascript the right way with  
Rails now. In the past any time it was mentioned it fell on deaf  
ears.

Cheers,

Dan

On 3/5/06, Sebastian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> framework that is all about beautiful code and strict separation, yet  
> implement all this manually for the sake of progressive enhancement.  
> eliminate inline JS yet keep within the friendly confines of JS/Ajax
> 
> Thanks.  
> Sebastian
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Dan W.  
[http://www.danwebb.net](http://www.danwebb.net)

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 6, 2006, 1:21am UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/3 "2006-03-06T01:21:27Z")

</div>

> Honestly, this i find quiet shocking. Rails is all about doing things  
> in a clean, strictly compartmentalized manner and Rails adopters  
> strike me as a very open-minded, progressive community. So, why take  
> steps backwards and turn a deaf ear to the important advancements on  
> this front? I’d recommend to everybody who honestly believes this is  
> just trivial nitpicking to read up on blogs like PPK’s http://  
> [www.quirksmode.org](http://www.quirksmode.org) and/or seek out the fine work Jeremy Keith, Dave  
> Shea and others have contributed to this subject.
> 
> Sebastian

I can think of a couple reasons why this isn’t part of rails:

- It’s not nearly as straight forward. Instead of using  
link\_to\_remote, you have to put some code in the head of the document  
or your behavior sheets.
- Browser issues. Setting the onclick of an anchor works great.  
However, adding event listeners doesn’t work because safari can’t stop  
the actual click from happening. This is probably a moot point if you  
just use $(‘link’).onclick = function() { …; return false; }.
- No one’s been able to demonstrate concrete code for it.

Dan: that plugin looks great. It would be nice to see it’s API match  
the RJS API, which has been going through major changes. You can now  
do:

page.select(‘#items a’).each do |anchor|  
anchor.add\_class\_name(‘foo’)  
end

I currently use Behavior sheets for Rails Weenie. Everything is page  
cached, so I have a request to a session javascript file that loads  
the appropriate sheet. Its all in javascript, but I was hoping to  
move it to RJS as part of a rendered view.

[http://techno-weenie.net/svn/projects/rails\_help/public/javascripts/rails\_help.js](http://techno-weenie.net/svn/projects/rails_help/public/javascripts/rails_help.js)

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 6, 2006, 12:54am UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/4 "2006-03-06T00:54:20Z")

</div>

On Mar 5, 2006, at 5:21 PM, Dan W. wrote:

> I’m about a few days of releasing a plugin which will solve your  
> problems and add a load of extra functionality to the rails javascript  
> helpers generally. As a bit of a sneak peek you can view the rdocs at  
> [http://www.vivabit.com/unobtrusivedocs/](http://www.vivabit.com/unobtrusivedocs/) Don’t try to install it yet  
> though because it’s not production ready yet. However, it when it is  
> I’ll post it on this list.

Dan, this is absolutely fantastic. Can’t wait for this to be  
officially released (any preliminary timeline?). In fact, i doubt i  
will do anything production-wise with Rails until your plugin can be  
used in a project. If this works as advertised, this would be an  
incredibly invaluable contribution. Please keep up the great work and  
let us know as soon you ready for people to start testing it.

> I’m glad that there are other people are  
> starting to become interested in doing javascript the right way with  
> Rails now. In the past any time it was mentioned it fell on deaf  
> ears.

Honestly, this i find quiet shocking. Rails is all about doing things  
in a clean, strictly compartmentalized manner and Rails adopters  
strike me as a very open-minded, progressive community. So, why take  
steps backwards and turn a deaf ear to the important advancements on  
this front? I’d recommend to everybody who honestly believes this is  
just trivial nitpicking to read up on blogs like PPK’s http://  
[www.quirksmode.org](http://www.quirksmode.org) and/or seek out the fine work Jeremy Keith, Dave  
Shea and others have contributed to this subject.

Sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 28, 2006, 1:50pm UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/5 "2006-03-28T13:50:00Z")

</div>

Hi,

Essentially some of Rick’s work on the JavascriptProxy class in 1.1  
collided with what I’m doing so my plan has been to stall until 1.1  
(…hmm, that would be now) then refactor what I have on top of 1.1  
and the Rick’s work which shouldn’t take too long. I got some  
valuable feedback from DHH and Rick which I plan to act on.

The good news is that there’s been some work on Prototype that makes  
the whole thing alot easier to manage. Bear with me, proper plugin  
will be released soon.

Cheers,

Dan

On 3/28/06, Sebastian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Honestly, this i find quiet shocking. Rails is all about doing things  
> > > I can think of a couple reasons why this isn’t part of rails:  
> > > Dan: that plugin looks great. It would be nice to see it’s API match  
> > > move it to RJS as part of a rendered view.  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Dan W.  
[http://www.danwebb.net](http://www.danwebb.net)

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 28, 2006, 10:36am UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/6 "2006-03-28T10:36:36Z")

</div>

Dan,

any progress on this? i saw the documentation was updated. does this  
mean it’s ready to go?

thanks,  
sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 28, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/7 "2006-03-28T20:14:18Z")

</div>

Thanks for the update, Dan.

Solving this still seems like a very important issue. Besides the  
problems implied, there’s also the annoying side effect of js helpers  
simply messing up well-formedness and validity in certain contexts.  
For instance, javascript helpers placed within lists (ul, ol, dl)  
will render pages invalid XHTML (strict):

\>Name: \<%= in\_place\_editor\_field :profile, :name, {:tag =\> "dd"} %\>

This (common enough) approach will place the script tags within the

 element, which will fail XHTML strict validation.

As i have gotten myself a little more aquatinted with Rails, i  
started playing around a bit to at least move specific code generated  
by the inline helpers to external files. This worked to a certain  
degree but in the end it’s only a newbie attempt and stopgap for now.  
Your plugin, however, seems to be shaping up to be a real solution.

Ideally, the plugin would be so low-level that it can work fully  
transparent in the background and not require any new declarations,  
changes in RJS or js helper syntax. Is that even possible? and, if  
yes, is that the route you are taking? Can you enlighten us a little  
how the re-worked plugin will be implemented?

As you can see, i’m really looking forward to this 🙂 I really  
appreciate your work.

Sebastian

---

<div class="post-metadata">

**Author:** ![feldpost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/d78d45/32.png) [@feldpost](https://www.ruby-forum.com/u/feldpost)\
**Post date:** [March 28, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/whatever-happend-to-unobtrusive-javascript-in-rails/77094/8 "2006-03-28T20:19:08Z")

</div>

if i complain about js helpers messing up validity, i should at least  
give an otherwise valid example :). Sorry.

It should be:

Name: \<%= in\_place\_editor\_field :profile, :name, {:tag =\> "dd"} %\>

sebastian
