Using [removed] How to create a 'Go Back' link that takes the user to a link if there's no history for the tab or window?

前端 未结 12 1885
星月不相逢
星月不相逢 2020-12-12 21:36

EDIT-2: None of the answers seem to work. Not even the one I previously marked as the answer of this question. Any help is appreciated. Thanks.

12条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 22:20

    Added a new answer to display the code formatted:

    The thing is that you were checking for document.referer, because you were in ff it was returning always true, then it was navigating to http://mysite.com. Try the following:

    function backAway(){
        if (document.referrer) {
            //firefox, chrome, etc..
            i = 0;
        } else {
            // under ie
            i = 1;
        }
        if (history.length>i)
        {
            // there are items in history property
            history.back();
        } else {
            window.location = 'http://www.mysite.com/';
        }
        return false;
    }
    

提交回复
热议问题