What's the best way to open new browser window?

前端 未结 10 1315
一生所求
一生所求 2021-01-30 03:06

I know that most links should be left up to the end-user to decide how to open, but we can\'t deny that there are times you almost \'have to\' force into a new window (for examp

10条回答
  •  野的像风
    2021-01-30 04:00

    I am using the last method you proposed. I add rel="external" or something similar and then use jQuery to iterate through all links and assign them a click handler:

    $(document).ready(function() {
      $('a[rel*=external]').click(function(){
        window.open($(this).attr('href'));
        return false; 
      });
    });
    

    I find this the best method because:

    • it is very clear semantically: you have a link to an external resource
    • it is standards-compliant
    • it degrades gracefully (you have a very simple link with regular href attribute)
    • it still allows user to middle-click the link and open it in new tab if they wish

提交回复
热议问题