简体   繁体   English

在Firefox中使用JavaScript刷新页面

[英]Refreshing a page with JavaScript in Firefox

I'm currently working on this really simple text based game and to play again, you have to refresh the page. 我目前正在开发这个非常简单的基于文本的游戏,要再次玩,您必须刷新页面。 This seems to work in Opera and Chrome, but it's not resetting the page in Firefox. 这似乎可以在Opera和Chrome中使用,但不能在Firefox中重置页面。 Here is the function I'm using. 这是我正在使用的功能。

$(function() {
    $('#play_again').click(function() {
        var answer = confirm ("Reset the game?")
        if (answer) {
        window.location.reload(); 
        }
    });
});

I think Firefox is caching the page or something. 我认为Firefox正在缓存页面或其他内容。 How could I make this work? 我该如何进行这项工作?

Instead of using window.location.href , I suggest using window.location.replace . 建议不要使用window.location.href ,而应使用window.location.replace Why? 为什么? Because href will add a new entry to the browser history. 因为href会将新条目添加到浏览器历史记录中。 This may not be the behavior you're expecting. 这可能不是您期望的行为。

window.location.replace( window.location.href )

this is something that's hard to do, but you can trick IE's and firefox's cache into thinking it's a new page by adding a random querystring. 这是很难做到的,但是您可以通过添加随机查询字符串来欺骗IE和firefox的缓存,使其认为这是一个新页面。 Can you try something like 你可以尝试类似的东西吗

window.location.href = window.location.href + '?refresh';

Try 尝试

$(function () {
    $('#play_again').click(function () {
        var answer = confirm("Reset the game?")
        if (answer) {
            $("form").each(function () {
                this.reset();
            });
            window.location.reload();
        }
    });
});

这有效:

document.location=document.location;

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

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