Difference between [removed].href and top.location.href

后端 未结 4 1950
轮回少年
轮回少年 2020-11-27 11:34

Can Anyone tell me the difference between window.location.href and top.location.href ?

And also where to use which one.

And which

4条回答
  •  星月不相逢
    2020-11-27 11:59

    The first one adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.

    The second replaces the current history item so you can't go back to it.

    See window.location:

    • assign(url): Load the document at the provided URL.

    • replace(url): Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

    window.location.href = url;
    

    is favoured over:

    window.location = url;
    

提交回复
热议问题