17

I am currently developing a web application where I need to open a popup window to show a report. The problem is that some versions of explorer don't support the window.open javascript function, so when this is the case I catch the error and open the new url with location.href. Here the code:

try {
    window.open(url, "","width=1002,height=700,location=0,menubar=0,scrollbars=1,status=1,resizable=0")
} catch(e) {
    location.target = "_blank";
    location.href = url;
}

The problem is that the location.target is not working and I would like to know if there is a way to specify the target of the location.href so it can be opened in a new tab.

user1084509
  • 1,832
  • 10
  • 33
  • 48

9 Answers9

27

try this one, which simulates a click on an anchor.

var a = document.createElement('a');
a.href='http://www.google.com';
a.target = '_blank';
document.body.appendChild(a);
a.click();
qiao
  • 17,941
  • 6
  • 57
  • 46
  • @T.J.Crowder I posted this answer after I tried it on jsfiddle: http://jsfiddle.net/XLLEk/ and it's working on my machine. You propably have prevented all popup windows in your browser, that's why it doesn't work in your case. If so, please consider remove the downvote, thanks. – qiao Jan 20 '12 at 16:48
  • 1
    qiao: You don't know who downvoted your answer. You know I commented, and you know someone downvoted. That's all you know; making assumptions based on that is a bad idea. Regarding the answer: Using default settings on Chrome, IE9, and Firefox, all three block the pop-up. Appropriately. So it doesn't offer anything on top of `window.open`. – T.J. Crowder Jan 20 '12 at 16:51
  • 1
    @T.J.Crowder Really sorry for the assumption. – qiao Jan 20 '12 at 16:55
  • qiao: No problem, when I was new to SO I made similar assumptions. Sometimes the assumption is right (it was in this case), sometimes -- frequently -- it's wrong. :-) – T.J. Crowder Jan 20 '12 at 16:57
  • 1
    @T.J.Crowder Thanks, it was really kind of you to teach me this lesson :) – qiao Jan 20 '12 at 16:58
  • Works for me in Chrome (and FF for that matter). – sbuck Jan 09 '13 at 20:38
  • I think it could easily lead to a downvote if you post an answer that assumes that the user does not block popups or that it is ok that the solution only works on one machine. – Anders Lindén Apr 13 '16 at 11:26
15

You can use this on any element where onclick works:

onclick="window.open('some.htm','_blank');"

digitalextremist
  • 5,952
  • 3
  • 43
  • 62
jojo
  • 161
  • 1
  • 2
12

The problem is that some versions of explorer don't support the window.open javascript function

Say what? Can you provide a reference for that statement? With respect, I think you must be mistaken. This works on IE6 and IE9, for instance.

Most modern browsers won't let your code use window.open except in direct response to a user event, in order to keep spam pop-ups and such at bay; perhaps that's what you're thinking of. As long as you only use window.open when responding to a user event, you should be fine using window.open — with all versions of IE.

There is no way to use location to open a new window. Just window.open or, of course, the user clicking a link with target="_blank".

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

If you go with the solution by @qiao, perhaps you would want to remove the appended child since the tab remains open and subsequent clicks would add more elements to the DOM.

// Code by @qiao
var a = document.createElement('a')
a.href = 'http://www.google.com'
a.target = '_blank'
document.body.appendChild(a)
a.click()
// Added code
document.body.removeChild(a)

Maybe someone could post a comment to his post, because I cannot.

Milos
  • 578
  • 6
  • 6
3

You could try this:

        <script type="text/javascript">
         function newWindow(url){
                window.open(url);
            }
        </script>

And call the function

bobbiloo
  • 422
  • 5
  • 22
2

If you are using an <a/> to trigger the report, you can try this approach. Instead of attempting to spawn a new window when window.open() fails, make the default scenario to open a new window via target (and prevent it if window.open() succeeds).

HTML

<a href="http://my/url" target="_blank" id="myLink">Link</a>

JS

var spawn = function (e) {
    try {
        window.open(this.href, "","width=1002,height=700,location=0,menubar=0,scrollbars=1,status=1,resizable=0")
        e.preventDefault(); // Or: return false;
    } catch(e) {
    // Allow the default event handler to take place
    }
}

document.getElementById("myLink").onclick = spawn;
cheeken
  • 33,663
  • 4
  • 35
  • 42
0

As of 2014, you can trigger the click on a <a/> tag. However, for security reasons, you have to do it in a click event handler, or the browser will tag it as a popup (some other events may allow you to safely trigger the opening).

jsFiddle

Maël Nison
  • 7,055
  • 7
  • 46
  • 77
0
<a href="url" target="_blank"> <input type="button" value="fake button" /> </a>
Paulo
  • 19
  • 2
0

Why not have a hidden anchor tag on the page with the target set as you need, then simulate clicking it when you need the pop out?

How can I simulate a click to an anchor tag?

This would work in the cases where the window.open did not work

Community
  • 1
  • 1
box86rowh
  • 3,415
  • 2
  • 26
  • 37
  • Not today, but I have used this technique in the past – box86rowh Jan 20 '12 at 16:48
  • I have to say I'm not buying it. Would you do a demo? Of this working, in modern browsers, without a user event? – T.J. Crowder Jan 20 '12 at 16:53
  • this probably wont work without user interaction..just tested it on a setTimeout..no go, pop up blocker caught it on chrome – box86rowh Jan 20 '12 at 17:00
  • Again: *Without* a user event. Your example happens in response to a click; you may as well us `window.open`. (I tried to do an updated fiddle, but http://jsfiddle.net is so slow I gave up. Your fiddle came up right away, but then jsfiddle disappeared for me so I can't even copy your code to http://jsbin.com -- much more reliable in my experience -- to test whether it works without the user event.) – T.J. Crowder Jan 20 '12 at 17:03
  • Ah, you commented while I was typing (and waiting for jsfiddle). – T.J. Crowder Jan 20 '12 at 17:04