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

后端 未结 4 1948
轮回少年
轮回少年 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:48

    window.location.href returns the location of the current page.

    top.location.href (which is an alias of window.top.location.href) returns the location of the topmost window in the window hierarchy. If a window has no parent, top is a reference to itself (in other words, window === window.top).

    top is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html with the following script:

    var newWin=window.open('about:blank','test','width=100,height=100');
    newWin.document.write('');
    

    The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href would return.

    To answer your question about redirecting, go with window.location.assign(url);

提交回复
热议问题