user34537
user34537

Reputation:

Prevent back button in browser?

I did 30+mins research and I can't figure out what the problem is. I'd like to prevent a user from hitting back in the browser and looking at the page again. How do I prevent it? I did tons of research and looking at the headers my bank sent out and still can't figure out what the problem is.

I must support firefox but having it work on other browsers is good too.

The headers I put out are (which I confirmed by looking at my what my browser thinks the response is)

Expires: Thu, 01 Jan 1970 00:00:00 GMT
Pragma: No-cache
Cache-Control: no-cache

In html I start with

<!DOCTYPE html>
<html>
<head>
    <title>The Title</title>
    <link href="/my.css" rel="stylesheet" type="text/css" />
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="EXPIRES" CONTENT="Thu, 01 Jan 1970 00:00:00 GMT">
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>

When I submit my form and hit back I can still see the page. I want to the browser to say document expired/invalid or have all the html reset so the form doesn't have user data still there. How do I do this?

Upvotes: 0

Views: 1348

Answers (1)

Dave Anderson
Dave Anderson

Reputation: 12294

Have a look at History.js by Benjamin Lupton. You can register an event handler to the onstatechange event and then prevent the default action. We use the jQuery adapter and handle our own history records because of Ajax requests with a method like this.

History.Adapter.bind(window, 'statechange', function() {
    var HistoryState = History.getState();
    if (HistoryState) {

        var stateData = HistoryState.data;

        if (stateData) {


        }
    }
});

You should be able to use e.preventdefault and stop the user going back.

We add the state data object for each page in the data object with History.pushState(state, null, ''); but not sure if you need any data to stop them going back.

Upvotes: 0

Related Questions