Skip to content

Instantly share code, notes, and snippets.

@navarr
Created February 2, 2010 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navarr/293176 to your computer and use it in GitHub Desktop.
Save navarr/293176 to your computer and use it in GitHub Desktop.
Open a Link in a New Tab using JavaScript
function newTab(url)
{
if(url.href) { url = url.href } // so that you can use onclick="return newTab(this);"
form = document.createElement("form");
form.setAttribute("method","get");
form.setAttribute("target","_blank");
form.setAttribute("action",url);
form.submit();
return false;
}
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<title>New Tab</title>
<script type="text/javascript" src="newTab.js"></script>
</head>
<body>
<a href="http://www.google.com/" onclick="return newTab(this);">Google (in a new tab!)</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment