使用JavaScript捕获浏览文件窗口的关闭 [英] Capturing the close of the browse for file window with JavaScript
本文介绍了使用JavaScript捕获浏览文件窗口的关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用infile要求用户浏览其计算机上的文件.如果没有选择文件就关闭了窗口,是否有办法捕捉?
例如,如果单击x.
I am using infile to ask the users to browse for a file on their machine. Is there way to catch if the window is closed without file being selected?
For example if x is clicked.
<input type="file" name="data" id="inFile" size="15" style="display:none" onchange="handleFiles(this)"/>
谢谢
推荐答案
我所做的是: 用户打开窗口后启动计时器,在该计时功能中,我每0.5秒检查一次是否已使用boolean var选择了文件.用户选择文件或HTML5 DnD发生时,我立即停止计时器.我希望这对某人有帮助.
What I did is: Start timer after the user opens the window, and in that timed function I check every 0.5 sec if the file has been selected with boolean var. I stop the timer as soon as user selects the file or HTML5 DnD occurs. I hope this helps someone.
<div id="uploader"><input type="file" name="data" id="inFile" size="15" style="display:none" onchange="handleFiles(this)"/></div>
<button dojoType="dijit.form.Button" id="fileSelect" type="button" onmouseup="browse();">Browse</button>
var fileselected = false;
function handleFiles(input){
fileselected = true;
//use input
}
var interval;
function browse(){
fileselected = false;
var fileElem = document.getElementById("inFile");
fileElem.click();
interval = setInterval(setdiv, 500);
}
function setdiv(){
if(!fileselected){
//do staff until user decides to Dnd or browse for file again
clearInterval(interval);
}
}
这篇关于使用JavaScript捕获浏览文件窗口的关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文