# Keeping javascript in the \<head\>

**URL:** <https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826>\
**Category:** Rails\
**Created:** [April 4, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826 "2006-04-04T17:07:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [April 4, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826/1 "2006-04-04T17:07:49Z")

</div>

Given that Rails has built in ways to do almost everything else 🙂 , is  
there a way to keep view specific javascripts inside the head tag of a  
layout without having to include that javascript on every page?

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [April 4, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826/2 "2006-04-04T18:06:55Z")

</div>

make your include tag dynamic. I can’t remember the exact syntax but  
you’ll get the picture.

\<%= include\_javacripts, \*\*\*\*\*\* #{@my\_javascript} %\>

in your view do something like this:  
\<% @my\_javascript = ‘view\_specifiic\_javascript.js’ %\>

Charlie B.  
[recentrambles.com](http://recentrambles.com)

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [April 6, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826/3 "2006-04-06T23:55:03Z")

</div>

In your , put:

 \<%= @content\_for\_page\_scripts %\> Then in your view: \<% content\_for("page\_scripts") do -%\> function item\_added() { var item = $('items').lastChild; new Effect.Highlight(item, { startcolor:'rgb(255,153,255)', endcolor:'rgb(200,200,200)' }); Element.hide('busy'); $('form-submit-button').disabled = false; $('item-new-code').value = ''; $('item-new-description').value = ''; Field.focus('item-new-code'); } function item\_loading() { $('form-submit-button').disabled = true; Element.show('busy'); } \<% end -%\> Then the stuff will be in the header on the delivered page, but close to where you need in in your .rhtml files. content\_for() works for other stuff, too! -Rob Rob B. http://agileconsultingllc.com removed\_email\_address@domain.invalid +1 513-295-4739

---

<div class="post-metadata">

**Author:** ![Marlon\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Marlon\_M](https://www.ruby-forum.com/u/Marlon_M)\
**Post date:** [April 8, 2006, 5:18am UTC](https://www.ruby-forum.com/t/keeping-javascript-in-the-head/56826/4 "2006-04-08T05:18:15Z")

</div>

thanks for the great tip. I had no idea about the @content\_for command.
