34

i have a page manageGroup.php, where user can also add member to group. I used colorbox to show the addGroupMember.php. Now i need to close that colorbox once i have done submitting the form.

javascript i am using in manageGroup.php

<script language="javascript" type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/dropdown.js"></script>
<script type="text/javascript" src="js/jquery.colorbox.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
 $(".iframe").colorbox({width:"80%", height:"80%", iframe:true});
 });
</script>

The link i am using to open colorbox

<a class="iframe" href="addGMember.php?id=<?php echo base64_encode($fetch->g_id)?>">Add Member</a>

the code in addGroupMember.php is like this:-

if($_POST['add']=="Go")
{
  $gid = $_POST['id'];
  $ii=0;
  $insert = "INSERT INTO ".DBGMEMBER." (gm_g_id,gm_m_id) VALUES ";
  foreach($_POST['gMember'] as $gMember)
  {
    if($ii==0)
    {
        $insert .= " ('".$gid."' , '".$gMember."')";
    }
    else
    {
        $insert .= " ,('".$gid."' , '".$gMember."')";   
    }
    $ii++;
  }
  $db->execute($insert);// after this i want to close the colorbox
  echo "<script>parent.$.fn.colorbox.close(); </script>";// code i used, but not working
}
Ashish Rajan
  • 1,182
  • 5
  • 14
  • 27
  • 1
    If it still doesn't work you can user something like `` and it should do the job. – Mr_Nizzle Oct 17 '11 at 17:14

9 Answers9

68

This one worked out perfectly for me and should work for you


parent.jQuery.colorbox.close()

Jack
  • 9,150
  • 3
  • 26
  • 33
Mr_Nizzle
  • 6,324
  • 11
  • 52
  • 85
16

i got it done for me, a bit crazy way, anyways u can too give it a try.

Supposing your page in iframe as x.php having form named xyz

<?php
  if($_post['submit']=='Submit')
  {
    //some php code here
    if(success)
     echo "<script>parent.$.fn.colorbox.close(); </script>";
    else
    {
      //some error handling here;
    }
  }
?>
<form name='xyz' action='x.php'>
 //some html code here
 <input type='Submit' name='submit' />
</form>
Ashish Rajan
  • 1,182
  • 5
  • 14
  • 27
8

First: Elaborate you question. The information you provided is some what shorthanded. There's no chance one could grip what you are doing. Also include some more sample code.

Only thing I could guess is that you trying to trigger the method in how it's written. Everything you add to the $.fn object is bound to all jQuery objects.

// doesn't work
$.fn.colorbox.close()
// proper way
$('idOfDomElement').colorbox.close()

..fredrik

fredrik
  • 17,177
  • 9
  • 46
  • 69
  • hi fredrik, i have posted an elaborated description to my problem. but i didn't got your point of using $('idOfDomElement').colorbox.close(). – Ashish Rajan Jan 20 '10 at 13:54
  • Could you please add the javascript code where you open the colorbox? – fredrik Jan 20 '10 at 14:09
  • Should be something along the lines of: $(".iframe").colorbox.close(); or parent.$(".iframe").colorbox.close(); – fredrik Jan 20 '10 at 15:40
4

Make sure that the page loaded within the iFrame. All of the necessary references to the colorbox js, jQuery js are not needed.

Then the parent close call will work:

parent.$('.yourElement').colorbox.close();
Gourneau
  • 11,760
  • 8
  • 41
  • 41
  • Actually, I just tried this on a blank page by just using and it worked! No headers, no other tags, nothing except the function. Using Chrome browser, dunno about IE or FF. – Derek May 04 '11 at 20:47
  • 1
    parent.$('#colorbox') selects properly (verified in firebug), but you won't get further. parent.$.fn.color.close() it is. (I wonder if colorbox is really 100% singleton in all scenarios, anyway it works...) – Frank Nocke Mar 13 '12 at 19:17
4

just this

parent.$.colorbox.close()

seems to work for me!

I used it like this without even a reference to any JS in the file loaded into the iframe.

  <a href="javascript:parent.$.colorbox.close()">close</a>

this alone in a blank HTML file seems to work.

steve
  • 93
  • 1
  • 7
2

I think the problem is that the colorbox belongs to the parent, not to the DOM in the iframe.

My guess is you'll need to call parent.[way to get the element or $.fn object].colorbox.close() or you'll need to add a function to the parent document and call parent.myCloseFunction()

rets
  • 21
  • 1
1

I just tried to close colorbox from within the iframe but couldn`t get it to work. I used $('#closebox').colorbox.close() and without any luck.

David
  • 11
  • 1
1

There is a simple solution actually which i use.

i put this <a id="btnDone" href="#">here</a> inside code or you can use your submit button's id and

 $(document).ready(function () {
        $("#btnDone").click(function () {
            window.parent.location = "../siparisler/listele.aspx"
        });
 }

this way u can redirect the parent page. and colorbox is automatically close. if you dont need redirection just put "#" for the window.parent.location

Ahmad Kayyali
  • 8,236
  • 13
  • 47
  • 83
Ahmet
  • 59
  • 1
  • 5
0

I did not manage to close the Colorbox with suggested solutions. I get parent undefined... So..


I found took a different approach:

Home document: (iframe: false) this will open the ajax_page in DIV in the same document

$(document).ready(function(){
    $("#modalwindow").colorbox({
        width:"800px",
        height:"510px",
        iframe:false,
        href:"../ajax_page.php",
    });
}); 

and in the ajax_page

$('#modalwindow').colorbox.close();

Hope this helps

Dr Casper Black
  • 6,882
  • 1
  • 24
  • 33