Jack Davis
Jack Davis

Reputation: 605

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. 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. How could I make this work?

Upvotes: 1

Views: 2630

Answers (4)

This works:

document.location=document.location;

Upvotes: 1

Jakob
Jakob

Reputation: 4854

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. Can you try something like

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

Upvotes: 3

Florian Margaine
Florian Margaine

Reputation: 60747

Instead of using window.location.href, I suggest using window.location.replace. Why? Because href will add a new entry to the browser history. This may not be the behavior you're expecting.

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

Upvotes: 4

Esailija
Esailija

Reputation: 140230

Try

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

Upvotes: 1

Related Questions