2

I am using window.open in jquery to open a link in a new tab. Works fine for me in chrome/safari/firefox, but it does not work in IE10.

$('.div').click(function() {
    $(this).target = "_blank";
    window.open('http://url/15M');
    return false;
});

How can I fix this?

agassi0430
  • 1,155
  • 1
  • 13
  • 31
  • 1
    This depends on the browser settings, and is not something you can force! As a sidenote, target is probably not a jQuery method, but a native property. – adeneo Apr 29 '13 at 19:32
  • possible duplicate of [Open url in new tab using javascript](http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript) – Ian Apr 29 '13 at 19:33
  • 1
    It's not possible to force/set with Javascript - it's up to the browser's settings. What are you trying to do with `$(this).target = "_blank";`? – Ian Apr 29 '13 at 19:33
  • @Ian the target is supposed to open the window in a new tab. This is my poor way of writing it. What Marek Lewandowski and mcpDESIGNS suggested is probably a much better way of doing it, however when clicked IE10 still does not open the link and treats it as href="#" – agassi0430 Apr 29 '13 at 19:41
  • @agassi0430 The way the answers suggest it is the *correct* way to open a new window/tab. It doesn't matter what "target" you set, you can't explicitly choose between opening a window or a tab. All you can do is say "Open whatever the browser's settings say to open". By default, most browsers open a new tab, but can easily be changed by the user. Long story short, you can control this with Javascript, as we've already pointed out, and as the duplicate question, I mentioned, has. In your code, `$(this)` refers to the specific `.div` element clicked, which is unrelated to `window.open` – Ian Apr 29 '13 at 19:46

2 Answers2

1

Try following:

$('.div').click(function() {
    window.open('http://url/15M', '_blank');
    return false;
});
Marek Lewandowski
  • 3,291
  • 1
  • 20
  • 26
  • 1
    Better way of writing what I was trying to do, however when clicked IE10 still does not open the link and treats it as href="#" – agassi0430 Apr 29 '13 at 19:42
1

The browser itself will decide when it's appropriate to open a new tab versus a new window, though you can influence its decision via browser settings. That being said, there are often times certain things we can do to encourage one way over the other. In this particular instance, I was able to get IE10 to open a window by passing along width and height values:

$("button").on("click", function () {
    window.open("http://msdn.microsoft.com", "popup", "width=640,height=480");
});

Keep in mind that you ultimately have no control over whether something opens in a new tab, or a new window. That is entirely up to the user's machine; so don't bake any user experience dependencies into this assumption.

Sampson
  • 265,109
  • 74
  • 539
  • 565