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 Dipanjan on July 8, 2009 - 9:16 pm
Quote
Sir,
I’ve used thickbox (modified) successfully in my website. You can browse the \Requests\ links and click on the \Read Posts\ link. An iframe based modal window will appear.
My problem is that the thickbox is taking too much time to get loaded. Is there any fixes for me? Thanx in advance.
#2 by Dharmavirsinh Jhala on July 9, 2009 - 9:58 am
Quote
Hi Dipan,
Basically the delay which is created is just because of contents you are loading in the thickbox using URL itself is slow.
Thickbox won’t show full page till all the contents are loaded, here I have observed that the way you are loading image is creating problem. Make thumbnail of uploaded image and you should see dramatic improvements in the load time of the thickbox window.
Let me know if it helps.
#3 by bobo on August 14, 2009 - 8:28 pm
Quote
Thanks for your tips.
In some cases, you may want to remove ability to close lightbox with esc key. In this, case comment out line tb_remove() after “if(keycode == 27)” in two places. Also remove the link with ID of TB_closeWindowButton in 3 places to remove link in upper right corner.
Today I want to add an Ajax call to my database to get data to fill the thickbox. The tb_init() function seems to be best place to put this, but I can’t figure out
what actually calls tb_init().
#4 by Fady on August 20, 2009 - 5:45 am
Quote
Dear : Dharmavirsinh Jhala
I use
Top.Window.Document.GetElemrntByID();
to catch the parent’s Controls And it’s Work
But i Admire your Way of solve , but i don’t Test it Yet ..
But Thanks in Advance … Sorry $ being late
Good bye
#5 by Angeline on August 31, 2009 - 2:18 pm
Quote
Hi, Your code is really good.I was searching how to use thickbox without href link, because I add an Id variable to the link, on clicking the link. This code works fine except for one problem. the first time I click the link,nothing appears,the pages goes black.(Coz I haven’t mentioned a href attribute in the anchor tag,only in the onclick function, I set the href attribute of the link). And only when I click the link for the second time, the required link comes up. Is there any way to solve this problem?
#6 by Angeline on August 31, 2009 - 4: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..
#7 by Jayme on September 2, 2009 - 5: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.
#8 by Angeline on September 2, 2009 - 10:03 am
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?
#9 by vivek on September 12, 2009 - 2: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.
#10 by Dharmavirsinh Jhala on September 13, 2009 - 1: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.
#11 by Ashish Taralekar on September 18, 2009 - 5: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.
#12 by GLV on October 9, 2009 - 10:13 pm
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!
#13 by GLV on October 9, 2009 - 10:16 pm
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].
#14 by charlie on December 10, 2009 - 8:58 pm
Quote
thanks #13, that’s exactly what i’ve been looking for.
#15 by amit bravo on December 24, 2009 - 3: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 ?
#16 by amit bravo on December 24, 2009 - 3: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’)
#17 by Dharmavirsinh Jhala on December 24, 2009 - 10:32 pm
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.
#18 by amit bravo on December 25, 2009 - 9:51 am
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
<a href="” id=”Btn”>
#19 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.
#20 by Pdfman on February 1, 2010 - 1:19 pm
Quote
that’s exactly what i’ve been looking for.