1

I am trying to do two things, 1. I need to open a new tab in the background without giving it focus, and 2. I need to be able to write to that page using javascript. (I only care about it working on Chrome)

Now if I use this code....

var handle = window.open("text/html", '_blank');
handle.document.write(reportPath);
handle.document.title = "Export Report";

I can open a new and write to it, HOWEVER, that window gets shoved in your face, which I do not want to happen, because it interrupts my user's workflow badly (I want to keep focus on the previous window).

However, I can use this code....

var a = document.createElement("a");
a.href = "about:blank";
a.target='_blank'
var evt = document.createEvent("MouseEvents");
//the tenth parameter of initMouseEvent sets ctrl key
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
a.dispatchEvent(evt);

Which works beautifully (it opens the new tab in the background), HOWEVER, I am unable to write to the new window that was opened because I cannot get the new window's handle.

Can somebody please tell me how I can either use the first method to open that window without giving it focus, or tell me how to use the second window, but somehow get the handle of that window so that I can write to it.

As I said before I only need this code to work for Chrome, it's a web app for my company and everybody here uses Chrome, so I don't care if it works with other browsers.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jared
  • 2,999
  • 1
  • 28
  • 39
  • Well, since you defined `var handle`, I would assume that would be the handle for the second window? Perhaps you could open the new window, write all the data to it, then hide it behind? – Felix Guo Dec 07 '12 at 17:56
  • 1
    Fair warning: You've said that the second codeblock using `dispatchEvent` works for you, opening the new tab in the background. It doesn't for me using Chrome 23.0.1271.95 on Linux, it opens it in the foreground: http://jsbin.com/eholeb/1 – T.J. Crowder Dec 07 '12 at 18:10
  • I'm using Chrome 24.0.1312.35 beta-m (windows 7 64 bit) and it works great, some other people at my office are using Chrome 22.0.1229.79 m and it works perfectly for them as well, maybe it's because you're using linux? :-P – Jared Dec 07 '12 at 18:22
  • @Jared: Could be! Just thought I'd give you the head's up. This isn't much help, but I recall a pop-under question and answer **somewhere** here on SO that (as of four months ago) worked on Chrome. So if you look hard enough... – T.J. Crowder Dec 07 '12 at 18:32
  • @Jared: I think [this was it](http://stackoverflow.com/questions/7924232/open-new-tab-in-javascript), and that doesn't work for me in Chrome 23. It also didn't in Chrome 14, but did in Chrome 15 (from the comments). This question is also probably a duplicate of that one, although this one is much more clearly and rigorously described, coded, and discussed. :-) Good luck, – T.J. Crowder Dec 07 '12 at 18:36

1 Answers1

0

Along with the answers above, you could also do a handle.blur() for the new window to lose focus.

tenky
  • 29
  • 3