Wasiim
Wasiim

Reputation: 146

How to refresh a parent page when closing an iframe - jquery

i need to refresh my webpage when the user close an iframe

var iframe = $('#layer-frame').contents();

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

I have the above code which doesnt seem to capture my click event. How to add the functionality of page refresh..please help

Upvotes: 1

Views: 3202

Answers (3)

Sridhar R
Sridhar R

Reputation: 20408

Try this

var ifra = $('#layer-frame').contents();
ifra.find(".close").click(function(){
  location.reload(true); // This will reload the current page
});

Upvotes: 0

Evgeniy
Evgeniy

Reputation: 2921

Here is some trick - you can try to catch frame bubbling click event -

Parent page

<html>
    <head>
        <title>testpage</title>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script>
            $(function(){
                var frame = window.frames[0] // first in may case

                $(frame).on("click", function(e){

                    if($(e.target).is('.close')){
                        window.location.reload();
                    }

                });
            })
        </script>
    </head>
    <body>
       <div><iframe src="iframe.html"/></div>
    </body>
</html>

iframe

<div class="root">
    <span class="close">click</span>
</div>

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

Write the code in iframe load function:

$('#layer-frame').load(function() {
   $(this).contents().find(".close").click(function(){
      alert("test");
      location.reload(true); 
   });
});

Upvotes: 1

Related Questions