How to close popup window on file download automatically?


Our company build web application client on top of electron.
The main window of our electron application has <webclient> tag inside of it where pages of our web application are loaded.
The tag has an attribute allowpopups to allow download files (reports from remote server in our case) with pop up windows. When we click download link on our application page (loaded into <webclient> tag) this js code (on client side) is executed:

    var reportParams = $('form#reportParams').serialize();

    var win =''+reportParams, '_blank');
    return false; // prevent default action

How to close popup window automatically when download begin? Google Chrome can do this.

How even get list of pop-up windows with electron api? How to handle download start which began on popup window?
Also it would be perfect if popup were hidden and close itself after
some timeout or on download start event.

electron v. 0.36.11


Are you talking about Electron, the application framework? Or something built on Electron?

To my knowledge, Electron doesn’t do pop-up windows on file downloads unless you make it do that. If you program it to create a pop-up window when downloading a file, then I would think you can keep a list of them.

I guess I’m not understanding the context of your questions.


thanks, @leedohm! I’ve edited the question.


Sorry @d9k, I’m not enough of a web programmer to know the answers to this question. It sounds like essentially you’re allowing Chromium to do the downloads and then you want programmatic access to that process? I’ll ping the developers and see if they have an answer.


If you want to manage the downloading of a file, there is the DownloadItem API that should allow you to do it. You would have to open the window using the standard Electron APIs though from what the developers are saying.


@leedohm thanks anyway.
We implemented our hack on top of client api (not complete code just to explain the idea. download handling not implemented yet, just printing from client page):

main window:
<webview src="" nodeintegration preload="./inject.js"></webview>


function PopupWindow(uid){
    this.uid = uid;
PopupWindow.prototype.print = function(){
    ipcRenderer.send('windowPrint', {uid: this.uid});
PopupWindow.prototype.close = function(){
    ipcRenderer.send('windowClose', {uid: this.uid});
PopupWindow.prototype.focus = function(){
    ipcRenderer.send('windowFocus', {uid: this.uid});
PopupWindow.prototype.blur = function(){
    ipcRenderer.send('windowBlur', {uid: this.uid});
}; = function(arg1, arg2){
    var windowInfo = ipcRenderer.sendSync('windowOpen', {
        url: arg1,
        target: arg2,
        domain: window.location.hostname
    var windowUid = windowInfo.uid;
    return new PopupWindow(windowUid);

electron server server.js:

const ipcMain = require('ipc-main');
var uuid = require('node-uuid');
var BrowserWindow = require('browser-window');

var mainWindow = null;
var popupWindows = {};

// . . . . . . various stuff like main window init . . . . . .

function composeUrl(domainUrl, endUrl){
    endUrl = endUrl.trim();
    var re = /:\/\//;
    if (!endUrl.match(re)){
        // trim function from
        return 'http://' + domainUrl + '/' + stringsHelper.ltrim(endUrl, '/');
    return endUrl;

ipcMain.on(ipcEvents.windowPrint, function(event, args){
    var popupWindow = popupWindows[args.uid];
    if (!popupWindow){
    var popupPage = popupWindow.webContents;
    if (popupPage.isLoading()){
        popupPage.on('did-finish-load', function(){
    } else {

ipcMain.on(ipcEvents.windowClose, function(event, args){
    var popupWindow = popupWindows[args.uid];
    if (!popupWindow){
    var popupPage = popupWindow.webContents;
    delete popupWindows[args.uid];

ipcMain.on(ipcEvents.windowOpen, function(event, args){
    var popupWindow = new BrowserWindow({});
    var newWindowUid = uuid.v4();
    var url = composeUrl(args.domain, args.url);
    popupWindows[newWindowUid] = popupWindow;
    event.returnValue = {uid: newWindowUid};