[removed].href vs clicking on an Anchor

前端 未结 6 715
鱼传尺愫
鱼传尺愫 2020-12-03 07:17

What\'s the difference between clicking on:


vs.

calling window.location.href = ...

?

6条回答
  •  心在旅途
    2020-12-03 08:12

    Setting window.location.href = 'thepage.html' is the same as calling:

    window.open('thepage.html', '_self');
    

    I.e. the target is limited to the same window, as that is where the location property is. This has the same effect as clicking a link without a target attribute:

    ...
    

    You can use the open method instead to specify a different target, like a new window:

    window.open('thepage.html', '_blank');
    

    This has the same effect as clicking a link with that target:

    ...
    

    You can also use the open method to open a new window. The return value is a reference to the window, so you can use that to set the location of that window instead of the current window:

    var w = window.open('about:blank', '_blank');
    w.location.href = 'thepage.html';
    

提交回复
热议问题