Error calling Javascript in Camping App

I want to learn about webapps. I decided to learn by doing and chose to start simple with Camping as (i). it is small & (ii). i know some ruby.

I think I am comfortable with the HTML and CSS side of things and using MAB. So i decided to step up and add some javascript which fails to work. Here is the offending code snippet from my View:

    div.image do
      link :rel => 'stylesheet', :href => 'styles.css'
      script :type =>"text/javascript", :src => 'display_date.js'
      # The hash argument always go at the end.
      button 'Display Date', :type => "button", :onclick => "displayDate" 
    end

The javascript file looks like this:

function displayDate()
{
 document.getElementById("demo").innerHTML=Date();
}

When I click the 'Display Date' button on the screen Firefox webconsole reports

-- [18:32:46.762] ReferenceError: displayDate is not defined

It does not work on IE either. The camping file and the Javascript file are in the same directory. Please advise. The result I get is however better than adding the Javascript inline which results in a stack error, I can live without inline js but it would be handy to know how to code this also.

Answers


I'm new to camping and Ruby too, so this may be horrible. It works though ;)

Give the controllers module a route to serve display_date.js to the client:

class MyJS < R '/display_date\.js'    
  JS = File.read('display_date.js')
  def get
    @headers['Content-Type'] = 'application/javascript; charset=utf-8'
    JS
  end
end

Call displayDate as a function, with parens: :onclick => displayDate().


To get the function to be called you must put in onclick = "javascript:displayData()"


Need Your Help

Looking for help with SAML Web Services and Netbeans 7

web-services netbeans saml netbeans-7

Google has failed me and Netbeans 7 is fairly new so I can't find any examples or tutorials yet. I'm using the sample Secure Calculator App that comes with Netbeans, its a simple web service with a...

How netbeans find error at time of coding

java netbeans

I wonder how netbeans and other IDE are able to detect error at time of writing code

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.