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

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 a Excel-file and save this at the desktop.

My code:

  • Detect mouse click location within canvas
  • PhantomJS cannot click on existing elements
  • Onclick javascript function working only on second click
  • How to handle a click on a <tr> but not on the child elements?
  • How to click an element in Selenium WebDriver using JavaScript
  • How to play/start youtube video after clicking on an image?
  • $('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: http://jsfiddle.net/0hnuw6b4/1/

    Does anyone have any idea why that does not work?

  • How can I pass ONLY clicks through a SVG with pointer-events?
  • jQuery click events firing multiple times
  • Having trouble with onchange and storing numbers in an array via textbox
  • Jquery .Click Function is Not Working Properly
  • Simulate real mouse click
  • localStorage store large size data
  • 2 Solutions collect form web for “Programmatical click on <a>-tag not working in Firefox”

    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
        document.body.appendChild(link);
        link.setAttribute("type", "hidden"); // make it hidden if needed
        link.download = 'test.xls';
        link.href = 'data:application/vnd.ms-excel;utf-8,test';
        link.click();
    });
    

    Fiddle

    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