Open a new tab with javascript but stay on current tab

http://stackoverflow.com/q/6213807/154877

by Marcel

HTML

<div id="wrap">
    <header>
        <h1>Open a new tab with javascript</h1>
    </header>
    <section>
        <p>Left click <a href="http://jsfiddle.net/TMHg4/show/" target="_blank">this link</a> to open it in the background.</p>
    </section>
    
    <footer>This is a code example for the Stack Overflow question <br><a href="http://stackoverflow.com/q/6213807/154877">"Open a new tab with javascript but stay on current tab"</a></footer>
</div>

CSS

body { color:#222; background:#fafafa; font:13px/1.4 'segoe ui', sans-serif; }
h1, h2 { margin:0 0 .25em; font:700 2.4em/1 'myriad pro', sans-serif; } h2 { color:#999; margin: 0 0 1em; font-size:1.6em; }
a { color:#06c; transition:.2s linear; -webkit-transition:.2s linear; } a:hover { color:#39f; }
p { margin:1em 0; } footer { color:#999; margin:3em 0 0; text-align:center; } footer a { color:#777; }
#wrap { width:500px; margin:1em auto; padding:1.5em; background:#fff; box-shadow:0 1em 2em rgba(0,0,0,.2); }

JavaScript

$("a").click(function(e){ e.preventDefault(); window.open(this.href); });