<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<input type="file">
<button id="brokenUploadButton">
Does not work!
</button>
<button id="customFileUploadButton">
Works
</button>
var brokenUploadButton = document.querySelector('#brokenUploadButton');
var customFileUploadButton = document.querySelector('#customFileUploadButton');
var file = document.querySelector('input');
//This does not work due to the securtity features prohibiting initiating the file browser window programmatically
brokenUploadButton.addEventListener('click', function(e) {
ajax(function(ajaxData) {
input.click()
});
});
//This works because it uses a timeout function as a hack
customFileUploadButton.addEventListener('click', function(e) {
var returnValueToCheck;
ajax(function(ajaxData) {
returnValueToCheck = ajaxData;
});
setTimeout(function() {
if (returnValueToCheck !== undefined) {
file.click()
} else {
console.log("Criteria not fulfilled")
}
}, 1000); //Timer should be larger than AJAX timeout
});
function ajax(callback) {
var xhr = new XMLHttpRequest();
xhr.timeout = 500;
url = "/echo/jsonp/";
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(xhr.responseText);
}
}
xhr.ontimeout = function(e) {
console.log("Timed out")
callback();
}
xhr.send();
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.