在shiny的应用中捕获iframe中的点击
我希望在shiny的应用中捕获iframe中的链接。 我想知道点击了哪个链接。
外面有光泽,这很好用。 我为相关问题添加了一个完全可重现的示例: https : xaamp
(它必须在(本地)服务器上运行,例如xaamp
)。
我的尝试:
1)要保存在Path/To/App
。
2)在www
文件夹中存储应该在iframe中显示的html文件。
fileWithLink.html
SOreadytohelp
3)必须使用runApp("Path/To/App", launch.browser = TRUE)
启动应用程序runApp("Path/To/App", launch.browser = TRUE)
(以便启动本地服务器)。
library(shiny) library(shinyjs) ui <- fluidPage( useShinyjs(), htmlOutput("filecontainer") ) server <- function(input, output, session){ session$onFlushed(once = T, function(){ runjs(" console.log('I arrive here') $('#filecontainer').load(function(){ console.log('But not here') var iframe = $('#filecontainer').contents(); iframe.find('#a').click(function(){ alert('I want to arrive here'); }); }); ") }) output$filecontainer <- renderUI({ tags$iframe(src = "fileWithLink.html", height = 600, width = 1200) }) } shinyApp(ui, server)
iframe包含在具有相关id( $('#filecontainer iframe')
)的div中。 有一个拼写错误调用锚标签。 我更改了目标以避免交叉脚本问题:
library(shiny) library(shinyjs) ui <- fluidPage( useShinyjs(), htmlOutput("filecontainer") ) server <- function(input, output, session){ session$onFlushed(once = T, function(){ runjs(" console.log('I arrive here') $('#filecontainer iframe').load(function(){ console.log('But not here') var iframe = $('#filecontainer iframe').contents(); iframe.find('a').click(function(){ console.log('am i here') alert('I want to arrive here'); }); }); ") }) output$filecontainer <- renderUI({ tags$iframe(src = "fileWithLink.html", height = 600, width = 1200) }) } shinyApp(ui, server)
fileWithLink.html
SOreadytohelp
Another page