Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trigger('click') Not Working in Chrome

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?

like image 448
pee2pee Avatar asked Feb 12 '26 15:02

pee2pee


1 Answers

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.

like image 145
Harry B Avatar answered Feb 15 '26 04:02

Harry B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!