0

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!

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.