在 Firefox 中使用 JavaScript 刷新页面

Refreshing a page with JavaScript in Firefox

本文关键字:刷新 JavaScript Firefox      更新时间:2023-09-26

我目前正在开发这个非常简单的基于文本的游戏,要再次玩,您必须刷新页面。这似乎适用于Opera和Chrome,但它不会在Firefox中重置页面。这是我正在使用的功能。

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

我认为火狐正在缓存页面或其他东西。我怎样才能做到这一点?

与其使用window.location.href,我建议使用window.location.replace。为什么?因为href会在浏览器历史记录中添加一个新条目。这可能不是您所期望的行为。

window.location.replace( window.location.href )
这是

很难做到的,但是您可以通过添加随机查询字符串来欺骗IE和Firefox的缓存,使其认为这是一个新页面。你能试试这样的东西吗

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

尝试

$(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;