How to use shiny javascript functions?

It does not work with $( document ).ready(function(), but with $( document ).on("shiny:sessioninitialized", function(event) {:

library(shiny)  
ui <- fluidPage(      
  HTML('<script>
       $( document ).on("shiny:sessioninitialized", function(event) {
           Shiny.onInputChange("too", "noone");           
       });</script>'),         
  textOutput("table")      
)

server <- function(input, output) {
  output$table <- renderPrint(input$too) 
}

shinyApp(ui,server)

Reason for this is given in the tutorial: You cannot call the function too soon, you need a little time until Shiny is ready to update the input value:

in message.js, we wrapped our code in $(document).ready(function() { ... }. This jQuery function will tell the browser to only run the code inside, once the page, i.e. the Document Object Model (DOM), is ready for JavaScript code to execute. Note that when we activate this code too soon, i.e. before the image is loaded, we cannot yet attach an event handler to it. In other words, here we want to be sure that the image exists before attaching an event handler to it. ```