Feedback

About Designerfoo and Me!

I am Manoj Sachwani, a webmaster, wannabe illustrator, designer, developer, amongst other things. I have over 14 years of experience, developing, designing and deploying websites. I am geeky, hairy and and I love what I do.

I stumbled & bought this domain - Designerfoo.com - because I thought it would be a "cool" domain to have, as time went on, I realized that I want to become - "Designerfoo" - and grow my knowledge in design, illustration, web design and web development.

I am webmaster, I design and develop websites with an edge. How may I help you?
Contact   -   @designerfoo   -   Facebook
Who is

jQuery: Thickbox Hack to refresh parent window on tb_close() event

DesignerfooDevelopment + jQuery • 09-12
Comment(s)

I was looking for a way to refresh the parent window on the remove event [tb_remove()] of thickbox jQuery plugin, developed by Cody Lindley, with/without modal and/or iframe popup windows. And here is what I did, hope this helps you guys too…

Step 1

Get the unpacked/unminified version of ThickBox.js. Open it up in your favorite text editor and find this function in the file.

function tb_remove() {
$("#TB_imageOff").unbind("click");
$("#TB_closeWindowButton").unbind("click");
$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,
#TB_HideSelect').trigger("unload").unbind().remove();});
$("#TB_load").remove();
if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
$("body","html").css({height: "auto", width: "auto"});
$("html").css("overflow","");
}
document.onkeydown = "";
document.onkeyup = "";
return false;
}

Now depending on how you want the close links or thickbox’s remove/close function to behave, in all of your pop-ups, you can modify the function to take up a true/false parameter, which when set to true would refresh the parent window, and false wouldn’t. This would give you some control over which thickbox pop-ups would refresh the parent window – this is what I have done.

Step 2-A: Modifying the function to accept parameters to refresh the parent.

We just edit the function to accept a parameter, and check if the passed variable is true or false. If true, refresh the parent window, if false, don’t, just close the window.


function tb_remove(closeparentwindow) {
if(closeparentwindow){
parent.location.reload(1);
}

$("#TB_imageOff").unbind("click");
$("#TB_closeWindowButton").unbind("click");
$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,
#TB_HideSelect').trigger("unload").unbind().remove();});
$("#TB_load").remove();
if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
$("body","html").css({height: "auto", width: "auto"});
$("html").css("overflow","");
}
document.onkeydown = "";
document.onkeyup = "";
return false;
}

That’s all there is to it :)

Now when you call the function tb_remove(), just pass the boolean value to the function and voila your parent window would refresh.

Calling the tb_remove() function to close thickbox’s parent window.

<a href=”#” onclick=”window.parent.tb_remove(true);”>Close</a>

Step 2-B: Modifying the function to refresh parent, everytime any thickbox window is closed.

function tb_remove() {
parent.location.reload(1);
//just add this line without the if and without modifying
the function to accept parameters

$("#TB_imageOff").unbind("click");
$("#TB_closeWindowButton").unbind("click");
$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,
#TB_HideSelect').trigger("unload").unbind().remove();});
$("#TB_load").remove();
if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
$("body","html").css({height: "auto", width: "auto"});
$("html").css("overflow","");
}
document.onkeydown = "";
document.onkeyup = "";
return false;
}

Note: Step 2B The above will refresh the parent window every time any kind of thickbox popup is closed, iframe,modal, etc. and is only advised if you know what your are doing.

Step 3: Save and you are done.

Post Rating 3.00 out of 5


Subscrive via feedburner rss

Subscrive via feedburner email

Some info + resources

Thickbox.js is not maintained anymore, albeit you can still use it and it works wondersand is pretty simple. Here are some replacements, you might want to consider.
  • jQuery Lightbox
  • Shadowbox - Framework Independent
  • jQuery UI's dialog box
  • 14 Responses to “jQuery: Thickbox Hack to refresh parent window on tb_close() event”
    Comment History Slider
      December 20, 2011 at 2:26 pm
      Author: andrea

      grazie

      October 14, 2011 at 8:08 am
      Author: ibrahim

      I am from Turkey. I not know english. But The paradise of God you.
      for Turkish
      Teşekkür ederim Dostum Allah seni cennetine alsın bu refresh işi çok işime yaradı.

      Very thanks :)

      August 3, 2011 at 2:00 am
      Author: Rebecca

      I have used the thickbox with a form in it and have got everything working. The only problem is the new onlclick=”window.parent.tb_remove(true);” completely bypasses the for validation and just refreshes. is there a way to have the thingo check to see if validation is true then refresh if not let the error message appear.

      Not up on the javascript lingo so please be gentle with me.

      Ta, Rebecca

        August 3, 2011 at 6:51 am
        Author: Designerfoo

        Hey Rebecca,

        Try validating the form first and capturing the boolean value in a var, only when the var is true then go ahead and window.parent.tb_remove(true); you can also mail me the code I can help with it :)

        for e.g.
        if(var)
        {
        //validation failed show error messages
        }
        else
        {
        //use jquery “live” binding or equivalent to bind/add
        window.parent.tb_remove(true);
        }

        hope this makes sense?

      June 28, 2011 at 6:43 am
      Author: ejay56

      for those who are using lightwindow.js – you may have been loosing all over for the modification required to make the parent window refresh on close.

      if ($(’lightwindow_title_bar_close_link’)) { Event.observe(’lightwindow_title_bar_close_link’, ‘click’, this.deactivate.bindAsEventListener(this)); $(’lightwindow_title_bar_close_link’).onclick =
      function()
      { parent.location.reload(1);
      return false;
      };
      }

      May 5, 2011 at 4:31 am
      Author: ira

      nice.. thanks for the script.. its really save my time (^_^)

      April 27, 2011 at 2:09 pm
      Author: Jagan

      This works like charm!!!

      Thanks for sharing… :)

      March 22, 2011 at 4:45 pm
      Author: marcin

      Thanks mate, that worked like a charm!!

      January 20, 2011 at 2:32 pm
      Author: jj

      In my case, I need to refresh by default, without modifying every instance calling the original function, but regarding the opposite as well via parameter. So I’ve implemented:

      function tb_remove(refreshparentwindow) {
      if(refreshparentwindow==null) refreshparentwindow=true;
      //this works in the case of missing parameter
      if(refreshparentwindow) parent.location.reload(1);
      ….

      September 14, 2010 at 3:28 am
      Author: fay

      you have exsample ?

      April 27, 2010 at 1:20 pm
      Author: Ali

      hi, your page is so nice and helpful.
      can you tell me how i can Redirect to specific page rather than the Refresh parent window.

      April 21, 2010 at 10:33 pm
      Author: vero

      hey, if need to exec an ajax function to relaod only one div.
      And if i exec that function insted of the “reload” works fine, but when i click again to open something else, it opens in a new window. Any idea why?

      February 9, 2010 at 5:38 pm
      Author: jdg

      Thanks! Just what I was looking for. This should be part of thickbox.



    Leave a reply :)

    Name:

    (required)

    Mail (will not be published):

    (required)

    Website:

    Comment:


    Subscribe

    Links

    Eventify – Simple Events

    NextGEN Resize Wordpress Plugin

    Categories

    Recent Posts

    Looking for something?

    Thanks to Subscribers

    • oops .. the whale has sunk. Twitter Status: Down!

    Thank you for taking the time out.

    Your Email (required)

    Your Email (required)

    Your Feedback

    Captcha
    captcha

    What to do you think of Eventify - Simple Events?

    View Results

    Loading ... Loading ...

    What do you think about the NextGEN Resize Plugin?

    View Results

    Loading ... Loading ...

    What do you think about the site design/UI/IA?

    View Results

    Loading ... Loading ...

      X Close Feedback.