Programmatical click on <a>-tag not working in Firefox

JavascriptJqueryFirefoxClick

Javascript Problem Overview


I have a problem with the click()-function from jquery. I create a <a>-element with document.createElement('a') and want call the click()-function about this element. About this element, I want to create an Excel-file and save this at the desktop.

My code:

$('body').on('click', '#test', function(event) {
    var link = document.createElement('a');
    link.download = 'test.xls';
    link.href = 'data:application/vnd.ms-excel;utf-8,test';
    link.click();
});

This function work under chrome, but not under Firefox.

Working example

Does anyone have any idea why that does not work?

Javascript Solutions


Solution 1 - Javascript

In Firefox, you can explicitly add the created element to the DOM and it will work:

$('body').on('click', '#test', function(event) {
    var link = document.createElement('a');
    // Add the element to the DOM
    link.setAttribute("type", "hidden"); // make it hidden if needed
    link.download = 'test.xls';
    link.href = 'data:application/vnd.ms-excel;utf-8,test';
    document.body.appendChild(link);
    link.click();
    link.remove();
});

Fiddle

Solution 2 - Javascript

You don't have to add the element to the DOM, even in FireFox. Replace the .click() method with the following code:

link.dispatchEvent(new MouseEvent(`click`, {bubbles: true, cancelable: true, view: window}));

$('button').on('click', function(event) {
    var link = document.createElement('a');
    link.download = 'test.xls';
    link.href = 'data:application/vnd.ms-excel;utf-8,test';
    link.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Download</button>

Solution 3 - Javascript

Add the element to the DOM before triggering the click:

document.body.appendChild(link);
link.click();
document.body.removeChild(link);

This worked for me in all the major browsers

Solution 4 - Javascript

You can use jquery for creating the element. It will work on both the browsers

$(document).on('click', '#test', function (event) {
    var link = $("<a/>", {
        "download": "test.xls",
        "href": "data:application/vnd.ms-excel;utf-8,test"
    });
    $("#test").append(link);
    link.get(0).click();
});

Fiddle

Solution 5 - Javascript

  var fileUrl=document.createElement('a');
      fileUrl.href=response.request.responseURL;
      document.body.appendChild(fileUrl);
      fileUrl.click();

add document body, thats working

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWhistleWhiteView Question on Stackoverflow
Solution 1 - JavascriptlurkerView Answer on Stackoverflow
Solution 2 - JavascriptDenis GiffelerView Answer on Stackoverflow
Solution 3 - JavascriptParulView Answer on Stackoverflow
Solution 4 - JavascriptAnoop Joshi PView Answer on Stackoverflow
Solution 5 - JavascriptAsanka SampathView Answer on Stackoverflow