在闪亮的应用中捕获iframe中的点击 [英] Capture click within iframe in a shiny app

查看:119
本文介绍了在闪亮的应用中捕获iframe中的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to capture a click on a link within an iframe in a shiny app. And i want to know which link was clicked.

外部闪亮这个工作正常。我为相关问题添加了一个完全可重现的示例:
https://stackoverflow.com/a/46093537/3502164
(它必须在(本地)服务器上运行,例如 xaamp )。

Outside shiny this works fine. I added a fully reproducible example for a related question: https://stackoverflow.com/a/46093537/3502164 (It has to run on a (local) server, e.g. xaamp).

我的尝试:

1)要保存在 Path / To / App 中的应用。

1) The app to be saved in Path/To/App.

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

2) In the www folder store the html file that should be displayed within iframe.

fileWithLink.html

<html>
<body>
<a href="https://stackoverflow.com/">SOreadytohelp</a>
</body>
</html>

3)应用程序必须以 runApp(Path / To /)启动App,launch.browser = TRUE)
(这样就启动了本地服务器)。

3) App has to be started with runApp("Path/To/App", launch.browser = TRUE) (So that a local server is started).

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的div( $('#filecontainer iframe'))。有一个拼写错误调用锚标签。我改变了目的地以避免交叉脚本问题:

The iframe is enclosed in a div with relevant id ($('#filecontainer iframe')). There is a typo calling the anchor tags. I changed destination to avoid cross scripting issues:

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

fileWithLink.html

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

anotherpage.html

anotherpage.html

<html>
<body>
Another page
</body>
</html>

这篇关于在闪亮的应用中捕获iframe中的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆