Javascript in Shiny tabPanel only runs once

This is a cross-post from an unanswered question on Stack Overflow.

The Shiny app below has two tabPanels, each with some Javascript within a sidebarPanel. I expect the Javascript in Tab 1 to run anytime Tab 1 is activated (i.e., when I initially launch the app and when I navigate back to Tab 1 from Tab 2). Also, I expect the Javascript in Tab 2 to run anytime I navigate to that tab.

Instead, the Javascript for both tabs runs immediately upon launching the app and then never again.

As context, I constructed this example because I ran into this problem while trying to use Javascript to place an Amazon Associates ad in my Shiny app.

Reproducible example

library(shiny)
    
    ui = navbarPage( "",
                      tabPanel( "Tab 1",
                                mainPanel(
                                  
                                  wellPanel("Blah blah blah"), 
                                  width = 6
                                ),
                                
                                sidebarPanel(
                                  
                                  # only runs once, like the ads
                                  HTML('<script type="text/javascript"> alert("Tab 1 is talking"); </script>')
                                  
                                  # # runs every time
                                  #HTML('<b> test </b>')
    
                                  , width=6 )
                      ),
                     
                      tabPanel( "Tab 2",
                                
                                mainPanel(
                                  wellPanel("Blah blah blah"), 
                                  width = 6
                                  
                                ),  # ends mainPanel
                                
                                sidebarPanel(
                                  
                                  # only runs once, like the ads
                                  HTML('<script type="text/javascript"> alert("Tab 2 is talking"); </script>')
                                  
                                  # # runs every time
                                  #HTML('<b> test </b>')
    
                                  , width=6 )
    
                                )
                      
    )
    
    server <- function(input, output) {
      
    }
    
    app = shinyApp( ui, server )