0

I have a js file in my assets/javascripts/ directory. This file is being picked up by rails correctly ( I can see it in firebug when I check the page's head tag)

And I'm calling a function from this js file in my application.js as follows:

//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

$(window).load(function(){
    Boxlayout.init();
});

I read on SO somewhere that the init() functions must be called with .load function so that they are called after the DOM is loaded.

But here the javascript is not working.

On the contrary when I explicitly include the js file with:

<%= javascript_include_tag "file.js" %>  

at the bottom of the page and call the same init function from application.js the javascript works fine.

I don't want to explicitly include the file. ( want to do it the rails way). So how do I do it with the first method?

EDIT

Using Rails 3.2.14 (No Turbolinks).

Also the Javascript is not working on Heroku though it works fine locally.

4
  • Show your application.js file, please Commented Sep 17, 2013 at 13:55
  • As an aside, .load(…) was deprecated in jQuery 1.8; you should be using .on("load", …). Commented Sep 17, 2013 at 14:09
  • Are you using turbolinks? If so, $(window).load will not be called. Turbolinks replace the body of the page, which is why your script is run when you embed it in the body. Commented Sep 17, 2013 at 14:12
  • @superluminary Nope. I'm using Rails 3.2.14 and no turbolinks. Commented Sep 17, 2013 at 14:14

3 Answers 3

1

Try changing

$(window).load(function(){

into

$(document).ready(function(){
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this means something different. load would wait until all images and CSS finish downloading, while ready runs as soon as the HTML has been downloaded and the DOM has been loaded into memory.
That still doesn't work when I remove the <%= include_tag %> for the js file
@RoryO'Kane I want to use the .load function. want to exceute the js after all css has been loaded.
0

You have //= require_tree . in application.js Just will try create new file *.js in assets/javascripts/ directory, and append your code there.

1 Comment

So what? I know that.
0

I think this is a turbolinks issue.

Turbolinks swap out the body of the page via AJAX when you click a link but leave the head intact. Because the CSS and JavaScript are only downloaded and parsed at the start of a browsing session you get a significant speed boost.

The downside is that your jQuery load events will not be triggered. This explains why your script is executing when you embed it in the body of the page, but not when it is correctly placed in the head.

Try including the jQuery turbolinks gem. This will trigger the expected jQuery events when a turbolink is activated.

https://github.com/kossnocorp/jquery.turbolinks

1 Comment

I'm not using Turbolinks.

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.