-1

I have a script that generates a csv file from html table data. The script below forces a download when the link class export is clicked. I would like this to happen automatically when the page loads. Is this something that is possible?

$(".export").on('click', function (event) {
    // CSV
    var args = [$('#output>table'), 'heartbeat.csv'];

    exportTableToCSV.apply(this, args);

    // If CSV, don't do event.preventDefault() or return false
    // We actually need this to be a typical hyperlink
});
Sean Kendle
  • 3,538
  • 1
  • 27
  • 34
KevMoe
  • 443
  • 4
  • 21
  • Possible duplicate of [How do I call a JavaScript function on page load?](http://stackoverflow.com/questions/3842614/how-do-i-call-a-javascript-function-on-page-load) – DibsyJr Feb 27 '17 at 17:01

1 Answers1

3

With jQuery, there is a .ready() listener function that you can set up like this:

$(document).ready(function(){
    var args = [$('#output>table'), 'heartbeat.csv'];    
    exportTableToCSV.apply(this, args);
});

I highly encourage you to review the documentation, here: https://learn.jquery.com/using-jquery-core/document-ready/

Sean Kendle
  • 3,538
  • 1
  • 27
  • 34