2017-09-07 35 views
8

मैं एक चमकदार ऐप में आईफ्रेम के भीतर एक लिंक पर एक क्लिक कैप्चर करना चाहता हूं। और मैं जानना चाहता हूं कि कौन सा लिंक क्लिक किया गया था।कैप्चर एक चमकदार ऐप में iframe के भीतर क्लिक करें

चमकदार के बाहर यह ठीक काम करता है। मैंने संबंधित प्रश्न के लिए एक पूरी तरह से पुन: उत्पन्न उदाहरण जोड़ा: https://stackoverflow.com/a/46093537/3502164 (इसे एक (स्थानीय) सर्वर पर चलाना है, उदाहरण के लिए xaamp)।

मेरे प्रयास:

1) एप्लिकेशन Path/To/App में सहेजने की।

2) www फ़ोल्डर में HTML फ़ाइल को स्टोर करें जो iframe के भीतर प्रदर्शित होना चाहिए। (ताकि एक स्थानीय सर्वर शुरू कर दिया है)

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

आइफ्रेम प्रासंगिक आईडी ($('#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> 
संबंधित मुद्दे