jQuery trigger('click') Not Working in Chrome

1.8k Views Asked by At

I previously had the following

$('#refresh').click(function () {
        $('#fileUpload').trigger('click');
});

However I've changed it so it should fire on a query parameter

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(function () {
    var param = getParameterByName('upload');
    if (param == "yes") {
        $('#fileUpload').trigger('click');
    };
});

It works in FireFox and IE but not Chrome. Any idea why?

1

There are 1 best solutions below

0
On

You can't trigger the action of a file upload input using JavaScript due to security limitations in some browsers.

So showing the file selection modal is not possible to prompt based on url.

An alternative would be to show your own light box / modal which contains only a strong call to action to upload followed by an input field or a button which masks an input field. Make it the primary and sole function of the page so as to make it as simple understand and to complete as possible.