html如何创建返回首页链接,使用JavaScript:如何创建一个“返回”链接,如果用户没有选项卡或窗口的历史,则将用户链接到一个链接上。...

its_me提出了一个问题:Using Javascript: How to create a 'Go Back' link that takes the user to a link if there's no history for the tab or window?,或许与您遇到的问题类似。

回答者NGLNZeta给出了该问题的处理方式:

You cannot check window.history.length as it contains the amount of pages in you visited in total in a given session:

window.history.length (Integer)

Read-only. Returns the number of elements in the session history, including the currently loaded page. For example, for a page loaded in a new tab this property returns 1. Cite 1

Lets say a user visits your page, clicks on some links and goes back:

www.mysite.com/index.html

www.mysite.com/about.html |

www.mysite.com/about.html#privacy |

www.mysite.com/terms.html

Now window.history.length is 4. You cannot traverse through the history items due to security reasons. Otherwise on could could read the user's history and get his online banking session id or other sensitive information.

You can set a timeout, that will enable you to act if the previous page isn't loaded in a given time. However, if the user has a slow Internet connection and the timeout is to short, this method will redirect him to your default location all the time:

window.goBack = function (e){

var defaultLocation = "http://www.mysite.com";

var oldHash = window.location.hash;

history.back(); // Try to go back

var newHash = window.location.hash;

/* If the previous page hasn't been loaded in a given time (in this case

* 1000ms) the user is redirected to the default location given above.

* This enables you to redirect the user to another page.

*

* However, you should check whether there was a referrer to the current

* site. This is a good indicator for a previous entry in the history

* session.

*

* Also you should check whether the old location differs only in the hash,

* e.g. /index.html#top --> /index.html# shouldn't redirect to the default

* location.

*/

if(

newHash === oldHash &&

(typeof(document.referrer) !== "string" || document.referrer === "")

){

window.setTimeout(function(){

// redirect to default location

window.location.href = defaultLocation;

},1000); // set timeout in ms

}

if(e){

if(e.preventDefault)

e.preventDefault();

if(e.preventPropagation)

e.preventPropagation();

}

return false; // stop event propagation and browser default event

}Go back!

Note that typeof(document.referrer) !== "string" is important, as browser vendors can disable the referrer due to security reasons (session hashes, custom GET URLs). But if we detect a referrer and it's empty, it's probaly save to say that there's no previous page (see note below). Still there could be some strange browser quirk going on, so it's safer to use the timeout than to use a simple redirection.

EDIT: Don't use ..., as this will add another entry to the session history. It's better to use a or some other element. Note that typeof document.referrer is always "string" and not empty if your page is inside of a (i)frame.

See also:

希望本文对你有帮助,欢迎支持JavaScript中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值