Reputation: 1
I am using rails 4 and trying to incorporate some simple javascript in my application. I believe the asset pipeline is working because I wrote alert(Hello!);
in the file pages.js.coffee, which is in my javascripts folder, and the alert worked on the web application. However, when I try to write something in the pages.js.coffee file like
`var today = new Date();
var hourNow = today.getHours();
var greeting;
if (hourNow >18) {
greeting = 'Good Evening!';
}
else if (hourNow > 12) {
greeting = 'Good Afternoon!';
}
else if (hourNow > 0) {
greeting = 'Good Morning!';
}
else {
greeting = 'Welcome';
}
document.write('<h3>' + greeting + '</h3>);`
nothing happens in to the application.
Furthermore, I am trying to incorporate this javascript in my HTML file using <%= javascript_include_tag "pages" %>
Could I also include the code using <script src"javascripts/pages.js.coffee></script>
Thanks for any help!
Upvotes: 0
Views: 30
Reputation: 4561
You need to wrap that js code in a document.ready call because the js files are loaded into the head of your document when using the asset pipeline and not at the bottom of the document like other platforms. This basically fires the code off before the DOM is fully constructed. –
As for including the code, yes either way is ok.
Upvotes: 1