简体   繁体   中英

Javascript not working on page refresh

So I have this block of code:

$(document).ready(function() {
  planSelectionForm.init($("form#new_account"));
});

And when I link to this page it works as expected. But when I refresh from the browser it doesn't get triggered. This seems like a common problem. Just for the record I'm using turbolinks. Any help on why this is happening would be great!

Wrap your .onready() functions into a function called initialize. The point, is to seperate the event-driven function calls such that the event driven function call calls a global function.

In there, add to your body or another element that supports onload.

$(document).ready(function() {
  initialize();
});
function initialize()
{
}

<body onload="initialize(); return;"> </body>

Also, for Caleb, in my experiance, I believe jQuery ready events only get executed on either a fresh load, or a ctrl+f5 cache reload.

The only solution I could find after getting this problem was wrapping my script with:

document.addEventListener("turbolinks:load", function() {
  planSelectionForm.init($("form#new_account"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM