开发者

How to detect back button and new window?

I've no doubt that these are probably documented on the internet somewhere and indexed by Google - but after wading through pages and pages of links to discu开发者_StackOverflow社区ssions about disabling the functionality I thought I'd ask to see if anyone can give useful answers about merely detecting their usage.

The method described in one of the better articles on disabling the back button only works by creating a page transition - so not much use for detection.

The presence of window.history.next might provide a mechanism for detecting the back button - but how widely supported is it? Does it require unusual privileges?

Jonathan's answer to this post looks promising - but pre-supposes that I can set the window name on the first landing - I guess I could use the absence of a cookie to detect first hit. Any other ideas?

TIA


I wrote some code a while back. The script was never fully tested, but you should be able to pull out some techniques to get the same effect as to detect the back button from it.

http://pastebin.no/32fx


Use sessionStorage/localStorage.

if (!sessionStorage.hasBeenHereBefore) sessionStorage.hasBeenHereBefore = true;
else {
    // Code to run if they've been here before.
}

If you use sessionStorage, then if they close the browser and come back, it will seem like the first time, but if you use localStorage then it'll stay forever.

You might try looking for some secret events like:

window.addEventListener('historyback', function () {}, false);
window.addEventListener('navigateback', function () {}, false);
window.addEventListener('returntopage', function () {}, false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜