仅在刷新页面后加载javascript [英] javascript loading only after refreshing page

查看:89
本文介绍了仅在刷新页面后加载javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 4.我有一个javascript文件app / assets / javascripts / exotic_fonts.js,它为字体添加了一些javascript功能。当我加载主页时,这个javascript文件会自动加载到主页上,但是它没有自动加载到其他页面上,例如/ user / show,并且缺少功能。但是,如果我刷新页面,则会加载javascript文件并返回功能。我想知道如何解决它。

I'm using Rails 4. I have a javascript file, app/assets/javascripts/exotic_fonts.js, which adds some javascript functionality to fonts. When I load the homepage this javascript file is automatically loaded on the homepage, however it's not loaded automatically on other pages such as /user/show and the functionality is missing. However if I refresh the page, then the javascript file is loaded and the functionality is back. I was wondering how I can fix it.

此外,是否可以只加载某个页面上的某些javascript文件?

Also, is it possible to load only some of the javascript files on a certain page?

谢谢。

推荐答案

您可以使用以下代码段来解决此问题。现在你的js代码将与turbolinks一起使用。

You can use following snippet to fix this issue. Now your js code will works with turbolinks.

ready = ->

// Your javascriptcode goes here

$(document).ready(ready)
$(document).on('page:load', ready)

这篇关于仅在刷新页面后加载javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆