在闪亮的应用中捕获iframe中的点击

Big*_*ist 8 javascript iframe jquery r shiny

我希望在闪亮的应用中捕获iframe中的链接.我想知道点击了哪个链接.

外面有光泽这很好用.我为相关问题添加了一个完全可重现的示例:https: //stackoverflow.com/a/46093537/3502164 (它必须在(本地)服务器上运行,例如xaamp).

我的尝试:

1)要保存的应用程序Path/To/App.

2)在www文件夹存储中应该在iframe中显示的html文件.

fileWithLink.html

<html>
<body>
<a href="https://stackoverflow.com/">SOreadytohelp</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

3)必须启动应用程序(以便启动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)
Run Code Online (Sandbox Code Playgroud)

jdh*_*son 5

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)
Run Code Online (Sandbox Code Playgroud)

fileWithLink.html

<html>
<body>
<a href="anotherpage.html">SOreadytohelp</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

anotherpage.html

<html>
<body>
Another page
</body>
</html>
Run Code Online (Sandbox Code Playgroud)