2017-09-07 29 views
8

我想捕获闪亮的应用程序中iframe内链接的点击。我想知道哪个链接被点击。在闪亮的应用程序中捕获点击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> 

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) 

回答

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) 

fileWithLink.html

<html> 
<body> 
<a href="anotherpage.html">SOreadytohelp</a> 
</body> 
</html> 

anotherpage.html

<html> 
<body> 
Another page 
</body> 
</html> 
相关问题