Test browser history before going back via javascript?

一笑奈何 提交于 2019-12-24 21:37:40

问题


I'm playing around with using some javascript to add extra functionality to a back button on my website. Right now I have a set of javascript that looks like this:

$(".back-link a").live("click", function(){
    history.go(-1);
    return false;
});

Now, it works great but I'm trying to make it as bulletproof as possible and one issue I foresee is that if someone lands on a project page and hasn't come to it via my home page then going back in their history one step will take them off my site. Obviously, this isn't what I want.

My guess is it would be a simple case of doing an if-statement but I'm not sure what or how to test for it. I suppose I could just test to make sure the base of the URL is my site but I'm not sure how.

Any tips or directions would be great.


回答1:


No, it's not at simple as that. You can use the history collection to go back or forward, but you can't get the URLs in the history.

The only information that you can obtain about where the user comes from is the HTTP_REFERER string in the request header, but you have to use server side code to get that.



来源:https://stackoverflow.com/questions/1514923/test-browser-history-before-going-back-via-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!