Keeping javascript in the <head>


#1

Given that Rails has built in ways to do almost everything else :slight_smile: , 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?


#2

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


#3

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

#4

thanks for the great tip. I had no idea about the @content_for command.