简体   繁体   English

Javascript无法进行页面刷新

[英]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. 仅出于记录目的,我正在使用turbolinks。 Any help on why this is happening would be great! 为什么会发生这种情况的任何帮助将是巨大的!

Wrap your .onready() functions into a function called initialize. 将您的.onready()函数包装到一个名为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. 在其中,添加到您的身体或其他支持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. 另外,对于Caleb,根据我的经验,我相信jQuery就绪事件只会在全新加载或ctrl + f5缓存重新加载时执行。

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"));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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