Run JS Script on Shiny Page Load

I'm working on R Shiny App with some JS code, and i'm able to get it execute after a click, but I'm looking for it to load with the UI runs. I've modify the custom.js function to .on('load'.. vs .on('click. It did not change the behavior. Any suggestions how to get the js code to execute on ui load with the Shiny app?

# ui.R
tags$script(src = "js/custom.js"),
downloadLink("downloadDataLink", "Download Data")
# custom.js
$( document ).ready(function() {
  $(document).on('load','#downloadDataLink',function() {
# server.R
  output$downloadDataLink <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")

    content = function(file) {
      file.copy("./data/data.csv", file)


I don’t have my laptop to test but this code probably raises an error in the JS console, saying Shiny does not exist yet. You should wait shiny to be ready with $(document).on(‘shiny:connected’, function() {}); … Below a reference with more information: Shiny - JavaScript Events in Shiny and Chapter 13 Shiny inputs lifecycles | Outstanding User Interfaces with Shiny.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.