Languages
[Edit]
EN

JavaScript - input file dialog closed event

10 points
Created by:
ArcadeParade
666

In this short article, we would like to show how for input file element, handle dialog closed event.

By default it is not possible to handle dialog close event in JavaScript. The solution presented in the article uses trick based on getting window focus back after file dialog is closed.

 

Focus based solution

The solution tracks window focus state and fires event when dialog is closed.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <input id="file" type="file" />
  <script>

    function addDialogClosedListener(input, callback) {
        var onFocus = function() {
            window.removeEventListener('focus', onFocus);
			callback();
        };
        var onClick = function() {
            window.addEventListener('focus', onFocus);
        };
        input.addEventListener('click', onClick);
        return function() {
            input.removeEventListener('click', onClick);
            window.removeEventListener('focus', onFocus);
        };
    }
    
    
    // Usage example:
    
    var file = document.querySelector('#file');

    addDialogClosedListener(file, function() {
        console.log('File dialog closed!');
    });
    
  </script>
</body>
</html>

 

Delay based solution

This section contains improved above solution.

It tracks window focus state and fires delayed event when dialog is closed. That solution helps to solve problems related with multiple dialogs (one after the other), e.g. directory upload dialog with displayed additional warning dialog.

The example shows how to handle dialog closed event on directory selection input. Select some big directory to see the effect.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <input id="file" webkitdirectory type="file" />
  <script>

    function addDialogClosedListener(input, callback) {
        var id = null;
        var active = false;
        var wrapper = function() {
            if (active) {
                active = false;
                callback();
            }
        };
        var cleanup = function() {
            clearTimeout(id);
        };
        var shedule = function(delay) {
            id = setTimeout(wrapper, delay);
        };
        var onFocus = function() {
            cleanup();
            shedule(1000); // change the value to bigger if needed
        };
        var onBlur = function() {
            cleanup();
        };
        var onClick = function() {
            cleanup();
            active = true;
        };
        var onChange = function() {
            cleanup();
            shedule(0);
        };
        input.addEventListener('click', onClick);
        input.addEventListener('change', onChange);
        window.addEventListener('focus', onFocus);
        window.addEventListener('blur', onBlur);
        return function() {
            input.removeEventListener('click', onClick);
            input.removeEventListener('change', onChange);
            window.removeEventListener('focus', onFocus);
            window.removeEventListener('blur', onBlur);
        };
    }
    
    
    // Usage example:
    
    var file = document.querySelector('#file');

    addDialogClosedListener(file, function() {
        console.log('Directory dialog closed!');
    });
    
  </script>
</body>
</html>

 

See also

  1. JavaScript - add event listener called only once

Alternative titles

  1. JavaScript - input file choicer closed event
  2. JavaScript - input file selector closed event
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join