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

前端 未结 10 1304
一生所求
一生所求 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 03:41

    If you use any flavor of strict doctype or the coming real xhtml-flavors, target isn't allowed ...

    Using transitional, whatever being HTML4.01 or XHTML1, you can use Damirs solution, though it fails to implement the windowName-property which is necessary in window.open():

    In plain html:

    <a href="..." target="_blank" onclick="window.open(this.href, 'newWin'); return false;">link</a>
    

    If however you use one of the strict doctypes your only way of opening links would be to use this solution without the target-attribute ...

    -- by the way, the number of non-js-browsers is often miscalculated, looking up the counters numbers refer very different numbers, and I'm wondering how many of those non-js-browsers is crawlers and the like !-)

    0 讨论(0)
  • 2021-01-30 03:44

    If I'm on a form page and clicking on a moreinfo.html link (for example) causes me to lose data unless I open it in a new tab/window, just tell me.

    You can trick me in to opening a new tab/window with window.open() or target="_blank", but I might have targets and pop-ups disabled. If JS, targets and pop-ups are required for you to trick me into opening a new window/tab, tell me before I get started on the form.

    Or, make links to another page a form request, so that when the visitor submits, the current form data is saved so they can continue from last time, if possible.

    0 讨论(0)
  • 2021-01-30 03:49

    Why is target="_blank" a bad idea?

    It's supposed to do exactly what you want.

    edit: (see comments) point taken, but I do think that using javascript to do such a task can lead to having some people quite upset (those who middle click to open on a new window by habit, and those who use a NoScript extension)

    0 讨论(0)
  • 2021-01-30 03:51

    I use this...

    $(function() {
      $("a:not([href^='"+window.location.hostname+"'])").click(function(){
        window.open(this.href);
        return false;
      }).attr("title", "Opens in a new window");
    });
    
    0 讨论(0)
  • 2021-01-30 03:53
    <a href="http://www.google.com" onclick="window.open(this.href); return false">
    

    This will still open the link (albeit in the same window) if the user has JS disabled. Otherwise it works exactly like target=blank, and it's easy to use as you just have to append the onclick function (perhaps by using JQuery) to all normal tags.

    0 讨论(0)
  • 2021-01-30 03:57
    <a href="http://some.website.com/" onclick="return !window.open( this.href );">link text</a>
    

    Details are described in my answer to another question.

    0 讨论(0)
提交回复
热议问题