162

Is there a way to make a link open a new browser window (not tab) without using javascript?

conbask
  • 9,741
  • 16
  • 57
  • 94

4 Answers4

280

That will open a new window, not tab (with JavaScript, but quite laconically):

<a href="print.html"  
    onclick="window.open('print.html', 
                         'newwindow', 
                         'width=300,height=250'); 
              return false;"
 >Print</a>
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ievgen Martynov
  • 7,870
  • 8
  • 36
  • 52
  • 9
    Depending on the OP's definition of "without javascript" (this answer uses JS but without including any JS files or – tobek Jan 14 '14 at 18:04
  • Is there any way to control where the new window is opened with this approach? It defaults to the to upper left corner, and I'd prefer it to be centered. (This is for a Twitter share intent, not spam.) – Kyle Clegg Feb 22 '14 at 00:18
  • 4
    @KyleClegg I think you would add the "top" and "left" attributes to the window.open() call, but it is up to you do decide how large the screen is. I don't believe there is an automatic way to center a new window. This link may help: [w3schools re window open](http://www.w3schools.com/jsref/met_win_open.asp) – Phil DD Apr 09 '14 at 18:12
  • Just tried this on three browsers. Worked on FF and Chrome, but not work on IE 11. – Josef.B Jul 28 '14 at 05:22
  • just tried it in IE11, works fine. great bit of code! – Tony Ray Tansley Oct 05 '15 at 17:35
  • 1
    For making it Resizable and having scrollbar just add....
    Fb
    – Anurag_BEHS Dec 27 '15 at 07:06
  • Worked for me in IE11 and latest Chrome 49 , cheers! Thanks – Gurpreet Singh Mar 09 '16 at 07:27
  • Doesn't work for me on IE11 (Exact version: 11.0.9600.18837, Update Versions: 11.0.48) – OfirD Dec 19 '17 at 09:17
  • 4
    You can also use only href, using `Print` – Maxwell s.c Jan 11 '18 at 20:15
  • 5
    Also if you don't want to repeat the link you can use: `onclick="window.open(this.href,'newwindow','width=1000,height=800'); return false;"` this uses the href of element `` – Koronos Jul 10 '18 at 21:13
  • It works on my PC, but not on my Android mobile. – user801347 Jan 23 '23 at 15:55
149

With pure HTML you can't influence this - every modern browser (= the user) has complete control over this behavior because it has been misused a lot in the past...

HTML option

You can open a new window (HTML4) or a new browsing context (HTML5). Browsing context in modern browsers is mostly "new tab" instead of "new window". You have no influence on that, and you can't "force" modern browsers to open a new window.

In order to do this, use the anchor element's attribute target[1]. The value you are looking for is _blank[2].

<a href="www.example.com/example.html" target="_blank">link text</a>

JavaScript option

Forcing a new window is possible via javascript - see Ievgen's excellent answer below for a javascript solution.

(!) However, be aware, that opening windows via javascript (if not done in the onclick event from an anchor element) are subject to getting blocked by popup blockers!

[1] This attribute dates back to the times when browsers did not have tabs and using framesets was state of the art. In the meantime, the functionality of this attribute has slightly changed (see MDN Docu)

[2] There are some other values which do not make much sense anymore (because they were designed with framesets in mind) like _parent, _self or _top.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • 3
    Seems to be not working in 2015. Safari 9.0 – Stefan Huska Sep 10 '15 at 11:21
  • @StefanHuska I think I explained that quite clearly in my answer: *and hope for the right browser settings* - If your browser is configured to open the link in a new tab, there is nothing you can do about it from the html perspective. That is not a matter of year or browser, most modern browsers open in a new tab nowadays. You need a javascript solution like Ievgen's for this. – Christoph Sep 15 '15 at 14:27
  • not working in google chrome and firefox (IE support this) – Joe Kdw Oct 14 '15 at 18:37
  • @JeanGkol I rewrote my answer to clarify the issue. Technically, it **is** working, because it opens a new browsing context (like it is supposed to do). The problem is, that in modern browsers "browsing context" is a tab instead of a window (you can change this in the settings though). – Christoph Oct 15 '15 at 14:14
39

I know that its bit old Q but if u get here by searching a solution so i got a nice one via jquery

  jQuery('a[target^="_new"]').click(function() {
    var width = window.innerWidth * 0.66 ;
    // define the height in
    var height = width * window.innerHeight / window.innerWidth ;
    // Ratio the hight to the width as the user screen ratio
    window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));

});

it will open all the <a target="_new"> in a new window

EDIT:

1st, I did some little changes in the original code now it open the new window perfectly followed the user screen ratio (for landscape desktops)

but, I would like to recommend you to use the following code that open the link in new tab if you in mobile (thanks to zvona answer in other question):

jQuery('a[target^="_new"]').click(function() {
    return openWindow(this.href);
}


function openWindow(url) {

    if (window.innerWidth <= 640) {
        // if width is smaller then 640px, create a temporary a elm that will open the link in new tab
        var a = document.createElement('a');
        a.setAttribute("href", url);
        a.setAttribute("target", "_blank");

        var dispatch = document.createEvent("HTMLEvents");
        dispatch.initEvent("click", true, true);

        a.dispatchEvent(dispatch);
    }
    else {
        var width = window.innerWidth * 0.66 ;
        // define the height in
        var height = width * window.innerHeight / window.innerWidth ;
        // Ratio the hight to the width as the user screen ratio
        window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
    }
    return false;
}
Community
  • 1
  • 1
Cuzi
  • 1,026
  • 10
  • 16
  • `top=300, left=350` Thank you for this line! :) – sohaiby May 07 '15 at 13:18
  • 2
    The OP requested "no javascript", but jQuery is based in javascript. – Sablefoste Jun 10 '15 at 03:17
  • You right, but either that there is no other solution and this is more elegant. it reflect the original behave of the document. if you want no javascript solution? you can just use the '_blank' like in Christoph answer. – Cuzi Jun 10 '15 at 03:19
  • return false; is necessary not just for the mobile solution. Otherwise this was a perfect fit for me – Ngodza Sep 03 '20 at 07:54
0

You can try this:-

   <a href="some.htm" target="_blank">Link Text</a>

and you can try this one also:-

  <a href="some.htm" onclick="if(!event.ctrlKey&&!window.opera){alert('Hold the Ctrl Key');return false;}else{return true;}" target="_blank">Link Text</a>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331