1

I am trying to present a new feature on a website already in production. Therefore, I am loading this website using an iframe into my development server as follows:

<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>
 <iframe id="myframe" src="https://production_website.com" style="border:none;" width="100%" height="600" seamless></iframe>
</body>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
     <script>
        console.log($("#myframe").contents());
        $("#myframe").contents().find(".importantContent").click(function(){
         console.log($(this));
        });
     </script>
</html>

I am trying to intercept user clicks on a specific DOM element in the iframe zone, based on that I'll be injecting a specific HTML content to demonstrate a feature. But listening with jquery click() is not working.

Any suggestions on what to do?

I am aware of the same origin policy restriction, but for a web designer/developer who doesn't have control over the server, aren't there workarounds like disabling the restriction using a pluging?

example: https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

Mohamed Ali JAMAOUI
  • 12,031
  • 10
  • 59
  • 101

1 Answers1

1

I found this answer in another thread, and it looks like it may work on your end. Try:

$('#myframe').load(function(){

    var iframe = $('#myframe').contents();

    iframe.find(".importantContent").click(function(){
           alert("test");
    });
});

Also, you have $("#reco").contents() instead of $("#myframe").contents(), were you meaning to not use the iframe you have in this example?

Trevor Yokum
  • 152
  • 9