-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When inserting javascript with insertUI, does it have to be wrapped in tags$head()? (Bug or feature?) #1545
Comments
This seems to be a minor bug. When you use Some reading material, in particular, this example that reproduces the same problem. His solution is pretty hacky (inserting and removing an image so you can use the |
We could reimplement insertAdjacentHTML using jquery's after, before, prepend, and append functions. |
Sometimes I wonder if I'm the only person who runs into this issue bcause once a year I come across this problem, google for a solution, and always end up back in this thread :) That's a subtle way to ping to see if this would perhaps make it into a future release if the "Effort: Low" label is correct |
@daattali not the only one :) We should probably do what @jcheng5 suggested years ago, but in the meantime, a workaround is to leverage ui <- fluidPage(
actionButton("go", "Insert javascript with an alert")
)
server <- function(input, output, session) {
observeEvent(input$go, {
insertUI(
selector = "head",
ui = htmltools::htmlDependency(
name = "fake-dep",
version = "1.0",
src = ".", package = "shiny",
head = "<script>alert('hello')</script>"
)
)
})
}
shinyApp(ui = ui, server = server) |
Consider this code that adds JS code that simply shows an alert box:
When you click the button, the javascript is added into the
<head>
tag of the DOM, but nothing happens.If you wrap the inserted UI in
tags$head(...)
, then you do get the javascript alert to show up.I don't know how the tags$head() works - I assumed that it simply inserts into the tag, but that would mean that the original code should also work. I guess there is some extra magic that happens for tags$head() that doesn't happen when I add it manually. Is it correct that JS code (and maybe other resources?) have to be wrapped in
tags$head()
in order for them to work?The text was updated successfully, but these errors were encountered: