Have you ever wished of doing something more with what you have been doing with jQuery thickbox? Now it has been more then 6 months that I am using jQuery on different projects. I have extended jQuery many a times during this period and to tell you one thing I have used jQuery for heavy-weight form processing for one of my very complicated project and it included processing of more then 1600 html controls with the help of great jQuery.
Now when it comes to Thickbox it gives nice functionality but many a times it leaves us hungry for more. For example here are the few situations where it will be difficult to use Thickbox without further modification.
- It let's open window in a IFRAME and close that but what if you can't give 'class="Thickbox"' to any of your link?
- What if you are not loading those links with page load? (If the links from which you are willing to open Thickbox windows is not available at the time of page load then Thickbox won't work. As Thickbox internally works in a way that it will search for all links with class = "Thickbox" at the time of page load and then customize their onclick events.)
- What if you want to open second Thickbox window as someone close first one and that too without any user interaction?
- What if you want to call any javascript function from the parent window? (parent window is the window from which parent window is getting opened.)
jQuery Thickbox is a great solution for many of our requirement but we don't have any option but to modify actual code to use them with complicated and advance situations which we come across.
I have done some modification in Thickbox.js to achive all above 4 points using Thickbox by adding 2 more functions into this great utility and modifying tb_remove function.
Here are 2 functions I have added, you can understand what they do once you read comments.
// To close one thickbox and then open another, this is quite handy // when you want to open another response modal window on the action of current modal window. // Opening more then one thickbox window is not possible on the same page, // so closing one and opening another could be quite handy and we can change height // and width as well for another window. var jThickboxNewLink; function tb_remove_open(reloadLink){ jThickboxReloadLink = reloadLink; tb_remove(); setTimeout("jThickboxNewLink();",500); return false; } // This function will let you open new thickbox window without specifying // class="Thickbox" and any href="http://web.com" attribute // It will be helpful when you are dynamically loading any content and // from those content you would like to open Thickbox windows. // As basically the nature of thickbox is such that it scans all links (..) tags // on load using jQuery's $(document).ready function so if your link is loaded using ajex // or using any dynamic javascript and was not on page at the time of load then this thickbox // setup won't work and you have to use this function. function tb_open_new(jThickboxNewLink){ tb_show(null,jThickboxNewLink,null); }
And here is the tb_remove function with minor modification which will let us call any function of the parent window passed as an argument.
// Modified to provide parent window's function callback function tb_remove(parent_func_callback) { $("#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",""); } if(parent_func_callback != undefined) eval("window."+parent_func_callback); document.onkeydown = ""; document.onkeyup = ""; return false; }
See Demo live in action or download zip with examples and modified javascript file.
Let me know if there are any other mods you have done with jQuery thickbox..! or if this post has helped you achieve what you wanted to do with jQuery Thickbox.
#1 by Angeline on August 31, 2009 - 9:24 pm
Quote
Hi , you had already given a solution in the previous comments which I didn't notice earlier. I added the line tb_open_new($(this).attr("href")); after changing the href link, and it works perfect now. The content is loaded in the first click itself.. Thanks a lot for your code..
#2 by Jayme on September 2, 2009 - 10:23 am
Quote
Using a slightly modified version of ThickBox. My client wants to be able to close the tbox from a link that will also redirect the user to a new page on his site.
1 – I can't even get the tbox to close from an inside link. I just reloads the content.
2 – Can it be made to refresh the parent page to a new URL?
Many thanks for your time.
J.
#3 by Angeline on September 2, 2009 - 3:03 pm
Quote
Hi, I tried your suggestion on adding the line tb_open_new($(this).attr("href")); when the urls are loaded dynamically in a javascript function. It works correctly, but when I click the same link for the second time without a page refresh, I get two thickbox windows,the top one has the content and the bottom one is empty. How to resolve this problem?
#4 by vivek on September 12, 2009 - 7:58 pm
Quote
Sir i am using thick box iframe it shows the close option in thickbox.js , but i want a link on my main page also to close the iframe.
#5 by Dharmavirsinh Jhala on September 13, 2009 - 6:58 pm
Quote
Hi Vivek,
If you are opening Thickbox main/parent window will not have focus and controls on parent window won't be clickable by users in that case putting up a Close link does not make sense to me on main/parent window.
If you want to put a close link on Child/Thickbox window then please use following code to close Thickbox from Thickbox model window itself.
onclick="javascript:self.parent.tb_remove();"
Let me know if you have any other question.
#6 by Ashish Taralekar on September 18, 2009 - 10:42 pm
Quote
Hi,
I am showing child window (contains an imagebutton, textboxes etc.) using thickbox and I am redirecting user to another page on click of that imagebutton using Response.Redirect. But, the problem is the page gets opened in the same thickbox. I want that page should open in the parent window.
So, please suggest me what should I do for this?
Thanks,
Ashish.
#7 by GLV on October 10, 2009 - 3:13 am
Quote
I'm getting an error ("missing ] after element list" ) with this code:
if(parent_func_callback != undefined)
eval("window."+parent_func_callback);
Couldn't figure out exactly what you were trying to do with this line so I commented it out and, lo and behold!, the thickbox still works as planned.
Could you shed some light on this, pls? Thanks!
#8 by GLV on October 10, 2009 - 3:16 am
Quote
For #11 Ashish Taralekar,
you could try using window.top.location.href = {your url}. That will make the parent window change locations instead of using server side code [Response.Redirect].
#9 by Britt on October 14, 2009 - 11:42 pm
Quote
I have two css running on one template thickbox and lightbox. I can't seem to get thickbox to work at all and I read your new code above but don't know where I should put it in the thickbox.css file.
#10 by charlie on December 11, 2009 - 1:58 am
Quote
thanks #13, that's exactly what i've been looking for.
#11 by amit bravo on December 24, 2009 - 8:54 pm
Quote
great work Dharma.. but actually we need a little more fix . the code Open Thickbox without href attribute
somtimes sucks if you webpage is long and link is in middle somewhere of the page when you click on the page though it opens thickbox but because of # .. this hash appears in the URL field of the browser and we are jumped in the begining of the page . sucks
if we try instead Open Thickbox without href attribute
then it works perfectly but there is also one thing that needs to be sorted out and that is what if a user disable java script of the browser . in this case i want my link like
Open Thickbox without href attribute
I think we can use function like preventDefault() somwhere is our function or any other trick and will stop to jump the page while javascript is enabled will help a lot
any trick ?
#12 by amit bravo on December 24, 2009 - 8:59 pm
Quote
I am sorry in the last comment the code i pasted converted in a link automatically .. please open view source to see the difference
or check this out >
a href="#" onclick="tb_open_new('window_one.html?TB_iframe=true&height=400&width=400&modal=true')
I used following to prevent hash problem that let us jump to begin of the web page which was looking ugly
a href="javascript: void(0);" onclick="tb_open_new('window_one.html?TB_iframe=true&height=400&width=400&modal=true')
but it would be great if somehow i could use a webpage link so that if javascript is disable yet link should work
a href="window_one.html" onclick="tb_open_new('window_one.html?TB_iframe=true&height=400&width=400&modal=true')
#13 by Dharmavirsinh Jhala on December 25, 2009 - 3:32 am
Quote
Hi Amit,
Thanks for helping me by looking at it with another direction.
Here is a simple solution to your problem.
Solution #1:
a href="http://www.digitss.com" onclick="return tb_open_new('www.digitss.com');"
In this case make sure to return false in "tb_open_new" function just like following:
function tb_open_new(jThickboxNewLink){
tb_show(null,jThickboxNewLink,null);
return false;
}
Solution #2:
a href="http://www.digitss.com" onclick="tb_open_new('www.digitss.com'); return false;"
And in that case no other change is required.
So All the Best.
Dharmavir.
#14 by amit bravo on December 25, 2009 - 2:51 pm
Quote
Thanks , Dharma ,its working
. though last night before your comment i tried this one and it works
$(document).ready(function(){
$("#Btn").click(function(event){
event.preventDefault();
tb_open_new('window_one.html?TB_iframe=true&height=400&width=400&modal=true');
});
});
where link was something like
#15 by senthilvelan on January 8, 2010 - 3:33 pm
Quote
Hi this is good work.
But i need one more thing just want to close the box once click \esc\ key or mouse click in overlay(Like in original thickbox),i think the problem is related to jquery function for this issue.
can you help me regarding this.
#16 by Pdfman on February 1, 2010 - 6:19 pm
Quote
that’s exactly what i’ve been looking for.
#17 by shap on March 4, 2010 - 12:03 pm
Quote
Hi,
Re#11 and #13. I can now close the thickbox from my button i have created, which is great…however, how can i now redirect the parent to a different url?
Cheers,
shap.
#18 by Thomas Fitzgerald on March 19, 2010 - 6:34 am
Quote
The ability to close a thickbox and automatically load another one is great. I am however having one issue. I am loading the thickbox using AJAX and not iFrame. The issue is that in the second page that gets loaded in a thickbox $(document).ready is not getting fired.
Any ideas?
Thanks for your help and the great thickbox extension.
Thomas
#19 by santhosh on June 20, 2010 - 6:28 pm
Quote
Hi can the call back function have arguments with it. I tried to send three arguments in the call back function to be called from the parent window but nothing happens. can u say whether the code u have writen handles arguments also or shud i modify the call back to accept arguments
#20 by Dharmavirsinh Jhala on June 26, 2010 - 3:40 pm
Quote
Hi Santosh,
The code does not handle arguments..
You need to modify the code to handle arguments.
Please update me once you do that.
Thanks.!
#21 by Doug on July 14, 2010 - 2:34 am
Quote
Another request for the ability to handle arguments or non-thickbox querystring parameters. That would really be useful.
#22 by Dharmavirsinh Jhala on July 14, 2010 - 5:06 am
Quote
Sure Doug,
Give me a time of 1 week. I will look into this request.
#23 by gusty guesty on August 23, 2010 - 7:35 pm
Quote
is there any way to close the modal window and have it automatically load a new (parent?) .html page?
thanks
#24 by gusty guesty on August 23, 2010 - 7:49 pm
Quote
eg. onclick="self.parent.tb_remove(); parent.location.reload('page2.html');"
#25 by gusty guesty on August 24, 2010 - 6:25 pm
Quote
FOUND IT!! if any one else needs to know how to load another page upon close window this works..
onclick="self.parent.tb_remove();self.parent.location='new_page.html'"
#26 by Dharmavirsinh Jhala on August 24, 2010 - 7:59 pm
Quote