Open a URL in a new tab (and not a new window) using JavaScript

I'm trying to open a URL in a new tab, as opposed to a popup window. I've seen related questions where the responses would look something like:

window.open(url,'_blank');
window.open(url);

But none of them worked for me, the browser still tried to open a popup window.


Nothing an author can do can choose to open in a new tab instead of a new window. It is a user preference.

CSS3 proposed target-new, but the specification was abandoned.

The reverse is not true; by specifying dimensions for the window in the third argument of window.open , you can trigger a new window when the preference is for tabs.


This is a trick,

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers, and the default "new window" behavior. You could do it this way, or by adding an event listener to your DOM object.

<div onclick="openInNewTab('www.test.com');">Something To Click On</div>

http://www.tutsplanet.com/open-url-new-tab-using-javascript-196/


window.open() will not open in a new tab if it is not happening on the actual click event. In the example given the URL is being opened on the actual click event. This will work provided the user has appropriate settings in the browser .

<a class="link">Link</a>
<script  type="text/javascript">
     $("a.link").on("click",function(){
         window.open('www.yourdomain.com','_blank');
     });
</script>

Similarly, if you are trying to do an Ajax call within the click function and want to open a window on success, ensure you are doing the Ajax call with the async : false option set.

链接地址: http://www.djcxy.com/p/18634.html

上一篇: 使用date.js在中午和午夜之间产生混乱

下一篇: 使用JavaScript在新标签(而不是新窗口)中打开一个URL