Jin Yong
Jin Yong

Reputation: 43778

Stop user to Shift+F5 on the Page

Does anybody here know how can I stop user to Shift+F5 or F5 (refresh the page) on the Page by JavaScript or jQuery?

The reason is just to stop the page to re-submit the same variable into the server side after the user clicked on the 'submit' button and then pressed F5 again.

Upvotes: 0

Views: 196

Answers (2)

keithjgrant
keithjgrant

Reputation: 12749

The answer isn't to stop a page refresh, it's to make it so a refresh doesn't POST the data again: do this by having your server script issue a redirect after processing the form. The browser will then GET the specified url (it might even be the same URL), so that a refresh no longer issues the POST, but the GET instead.

Upvotes: 2

Zubair1
Zubair1

Reputation: 2780

EDITED.. Sad to say but there isn't really better solution than this.

<script type="text/javascript">
    window.onbeforeunload = function(event)
    {
        event = event || window.event;

        // For IE and Firefox (older versions)
        if (event) {
            event.returnValue = 'Are you sure you want to do this?';
        }

        // For Safari
        return 'Are you sure you want to do this?';
    }
</script>

--- Just wanted to add the following:

Hey, I've been playing around with the code i posted above.. i wasn't able to notice that it was actually still refreshing.. the only possible solution is to raise the popup dialog, which confirms from the user if they really want to leave/refresh the page.

I'm updating my code to reflect what i just said.

Upvotes: 0

Related Questions