jquery, thickbox and iframe

I am using jquery's thickbox 3.1 for an image gallery. My problem started because the previous developer made the site using iframes. I strong recommend you not using iframes or frames, and there are many reasons for you to not using them.

Of course this is a hack, and the best scenario you shouldn't use frames and then do the coding in "perfect" manner.

In order to make the thickbox work inside an iframe you must do the following:

1 - Add jquery and thickbox files to both main and iframe pages. This is required so the previous and next link works on the inline popup (thickbox).

2 - Make the following changes to the thickbox.js file

2.1. - Replace the code inside the function tb_init(domChunk) tb_show(t,a,g); to self.parent.tb_show(t,a,g,window);

2.2. - Replace function TB_show(caption, url, imageGroup) to function TB_show(caption, url, imageGroup, refWindow)

2.3. - Replace TB_TempArray = $("a[@rel="+imageGroup+"]").get(); to TB_TempArray = refWindow.$("a[@rel="+imageGroup+"]").get();

2.4. - Replace tb_show(TB_PrevCaption, TB_PrevURL, imageGroup); to tb_show(TB_PrevCaption, TB_PrevURL, imageGroup, refWindow);

2.5. - Replace tb_show(TB_NextCaption, TB_NextURL, imageGroup); to tb_show(TB_NextCaption, TB_NextURL, imageGroup, refWindow);

3 - Now the code for your links must be something like this:

<a href="pathToBigImage/bigImageName.jpg" class="thickbox" rel="yourGalleryName" onclick="self.parent.tb_show((this.title || this.name || null),this.href,(this.rel || false), window);return false;"><img src="pathToSmallImage/smallImageName.jpg" alt="Image Title" title="Image Title" border="0"/></a>

That's all you need. It took me while to figure this out. On the internet I found some information but it was confusing so I decided to compile everything in one post. I hope this helps anyone looking to do the same.

Comments
Adam Luz's Gravatar This fix did not work for me. Is it possible you could upload your js file and offer it for download?

Thanks
Adam
PS. Your comment box is acting weird. If I don't use enter it does not go to a new line and I can't see my text anymore. Might want to shorten the width. IE7 :)
# Posted By Adam Luz | 9/28/07 1:22 PM
Emanuel Costa's Gravatar Hi Adam, you can download the jquery and thickbox files I am using here:
http://blog.emanuelcosta.com/tests/jquery_thickbox... and if you want to see the files in production go to http://www.sojodesign.com and click the porfolio link.
Thanks for the information on the comment box, on firefox works fine :)
Regards
# Posted By Emanuel Costa | 9/29/07 12:32 AM
Sarah's Gravatar Thank you for the time you've clearly put into this, but after trying to implement your changes three times (each after re-downloading a fresh set of the thickbox.css, thickbox.js, and jquery.js files), I'm still having problems.

The thickbox is now outside the iframe and in the parent window, but, it's displaying on the left-hand side of the screen, with half of the image off-screen. The screen overlay effect, the image caption, next/previous buttons, and close options aren't showing up either.

Any ideas? The website you linked is beautiful, by the way.
# Posted By Sarah | 9/29/07 3:49 PM
Emanuel Costa's Gravatar Hi Sarah, I don't know on top of my head what could be the problem on your page. Can you give me the link to your gallery? Cheers
# Posted By Emanuel Costa | 9/29/07 6:22 PM
Zuriel's Gravatar I am trying to get thickbox to invoke from a Frameset, the middle frame has a link that pops up a iframe. I am unable to get this working once the middle frame runs from inside the frameset.

if i pull the frameset apart and just open middle frame by itself, it works perfectly.

the 2 things that don't work are

1) the overlay does not cover the entire frameset. there are 150 pixels at the top of the page that aren't covered because of the top navigation frame.

2) the self.parent.tb_remove(); code does not work inside my iframe to close the thickbox.
the code worked when the page loads by itself, but once the frameset loads self.parent no longer is pointing to the correct thing...

any ideas?
# Posted By Zuriel | 10/9/07 8:51 AM
Zuriel's Gravatar i want to add that what I am asking has nothing to do with the code you posted... this is just using the default thickbox. i just figured you could potentially know since you just got done writing code with a similar issue.
# Posted By Zuriel | 10/9/07 8:53 AM
francis's Gravatar this looks like the solution I'm after (trying to open a thickbox containing a flash video display from within an iframe) but I'm a complete novice at js - can you give me an indication of the values I should associate with this string? onclick="self.parent.tb_show((this.title || this.name || null),this.href,(this.rel || false), window);return false;">
# Posted By francis | 10/16/07 10:20 PM
Nate's Gravatar I'm struggling with something similar, but not quite the same.
I want to make a Thickbox with the iframe query parameter append html in a div of the parent window
I've tried many things and nothing seems to work.

Here's the url that opens the thickbox:

/test/index.cfm?height=422&width=465&productID=9&TB_iframe=true&TB_iframe=true

Here's a few things that I've tried in this thickbox window that don't
work:

console.log($(this).parent('#numberofCartItems').text());
$(this).('#numberofCartItems',parent).empty();
$('#numberofCartItems',parent).empty();
$('#numberofCartItems').empty();
$('#numberofCartItems').parent().empty().append("1");
console.log($('div:contains("3")').text());
console.log($(this).parents('div'));
# Posted By Nate | 10/24/07 2:05 PM
Paul @ miniBB's Gravatar I have posted a solution on Thickbox forums which worked for me in all aspects.

http://codylindley.com/thickboxforum/comments.php?...

The basic idea behind it all is that Thickbox must initiate all functions in the parent window straight away. However these functions will all refer to the iframe content. Explained how it could be done for iframe and parent window (yes, the solution above misses some points btw).
# Posted By Paul @ miniBB | 1/15/08 11:29 AM
# Posted By Jake | 2/20/08 10:56 AM
janus's Gravatar Hi, i've got bit of the same problem as Sarah.
My image get enlarged outside the iframe-wich is good- but the buttons of the thickbox and the overlay are both gone. Does anyone has a solution?

Thanx!
# Posted By janus | 8/17/08 2:26 AM
Eamon's Gravatar This worked a treat followed the code verbatim, had a link inside a google map i wanted to open into a thickbox
# Posted By Eamon | 9/16/08 2:21 AM
becky's Gravatar I'm sorry I'm still kind of new at this- but in the section where you wrote "your code for your links must be something like this:

am i supposed to change "this.href" or "this.name" to something? or do i just leave the code how you have it???
# Posted By becky | 9/24/08 11:58 AM
smithveg-backpack's Gravatar I've thickbox installed in my system. But the thickbox always display behind of my flash banner. Any ideas how can i solve it?
# Posted By smithveg-backpack | 12/14/08 6:11 AM
razvan pataca's Gravatar here is the file emanuel is using... i just copied his and everything works great.

http://www.sojodesign.com/jquery/thickbox.js
# Posted By razvan pataca | 12/15/08 7:57 AM

Consumed By Feed-Squirrel.com