2017-09-07 4 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'))で囲まれています。アンカータグを呼び出すタイプミスがあります。

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

anotherpage.html

<html> 
<body> 
Another page 
</body> 
</html> 
fileWithLink.html

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) 

:私はクロススクリプティングの問題を回避するために、目的地を変更しました

関連する問題