window.history.back()但仅当不会将用户带离当前站点时

问题描述 投票:0回答:1

标题说得很好。即我需要调用它来将用户带到他们所在的先前位置,但只有当它不会将他们带离我们的网站时。

EG

mysite.com/#/foo - > 
mysite.com/#/bar - > 
back is triggered - >
user ends back to mysite.com/#/foo

好像用户直接去的地方

mysite.com/#/bar - > 
back is triggered - >
user is back to whatever site they were first in. 
javascript html5 browser-history html5-history
1个回答
0
投票

您可以使用简单的功能:

function returnBack(){

    // If last visited page before this page is in your domain
    if(document.referrer.indexOf(window.location.host) >= 0)
    {
        window.history.back()
    }
    else {
        // If last page is not in your domain, do your logic here..
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.