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 1874
星月不相逢
星月不相逢 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:24

    this seems to do the trick:

    function goBackOrGoToUrl() {    
    
        window.history.back();
        window.location = "http://example.com";
    
    }
    

    Call history.back() and then change the location. If the browser is able to go back in history it won't be able to get to the next statement. If it's not able to go back, it'll go to the location specified.

提交回复
热议问题