Boxen – a jQuery iframe plugin
As mentioned in a previous post, I’m a big fan of displaying small, configuration style screens in Lightbox style windows. If you’re not familiar with Lightbox, check it out here.
The thing with configuration screens is they usually have a little more to do than simply display an image or other static content. In fact, most of the ones I’ve written require some sort of AJAX functionality. Using an iframe is perfect for these scenarios as they operate as a self contained window, complete with any custom JavaScript.
I began using Cody Lindley’s ThickBox and more recently DOMWindow to load my iframes dynamically. Whist both of these plugins are great, I found they lacked the ability to truly style the boxes and extending them to add callbacks and extra features was not possible without editing the core JavaScript. I was after a simple yet expandable solution to dynamically open an iframe in a Lightbox style window.
Boxen
The boxen plugin allows you to bind a click event to any element that will open a Lightbox style iframe.
Example
$('.boxen').boxen(); $('#boxen_google').boxen('http://www.google.com/'); $('#boxen_search').boxen('http://www.google.com/search', { urlParams: { q: 'More Cowbell' } });
Documentation
boxen([url or options], [options])
Arguments
url or options (optional) – String, Options
If the first parameter is a string, it is used as the URL for the iframe source. Otherwise, it is treated as the options map.
options (optional) – Options
urlParams: {}, showTitleBar: true, showCloseButton: true, title: null, titleAttribute: 'title', closeButtonText: null, width: 600, height: 500, url: null, urlAttribute: 'href', overlayOpacity: 0.8, overlayColor: null, modal: false, postOpen: function(contentAreaElement) {}, postClose: function() {}
Demo
Download
- Boxen-1.4.tar.gz (13k)
- Boxen-1.4.zip (14k)
UPDATE 2009-03-20
Boxen now has the ability to open an empty canvas. Simply cancel out any URL by setting the url or urlAttribute options to null. See the demo page for a nifty Google Maps example.
UPDATE 2009-04-06
Boxen 1.2 is now IE6 compatible. Due to the lack of position: fixed, the experience may be a little jerky / flickery when the window is resized and occasionally the transparent overlay may be a few pixels too long or too short however I think this is a case of “near enough is good enough”.
Unfortunately, I’ve had to alter the Boxen “id” attribute naming convention and remove the leading underscore to get it to work in IE6. If you’ve used any custom CSS rules, you’ll need to edit them accordingly.
Also fixed in this version:
- BIG iFrame implementation for overlaying IE6 form controls
- Safari iFrame ID caching bug
- Opera opacity issue
I’ve only tested this using a STRICT doctype and encourage everyone else to do so too.
UPDATE 2009-10-08
Boxen 1.3 contains the following updates:
- Fixed “frameBorder” case sensitivity (thanks Caleb)
- Added more streamlined way to anonymously open a Boxen window
$.Boxen.open(null, { url:'http://www.google.com/', title:'Boxen window title' });
- Now released under the MIT license
UPDATE 2009-11-30
Boxen 1.4 contains the following updates
- Added “overlayColor” option
- Stylesheet example now includes a “spinner” loading animation (courtesy of http://www.ajaxload.info/). Provided your iFrame / content specifies a background, the spinner will not be visible once the content loads


February 21st, 2009 at 12:55 am
Nicely done. Just to clarify.
DOMWindow does have callbacks (functionCallOnClose and functionCallOnOpen) and I have left the styling of the box (except for simple styling) to the use of CSS and not function parameters.
But, I like the fact that you created your own. Its always better and you are better when you take something like DOMwindow and then make it your own.
Again, Nice work.
February 23rd, 2009 at 12:00 pm
Hey, thanks Cody. You know, I must have pulled apart the DOMWindow code 20 times and not seen those callbacks. I’m still using DOMWindow for inline and simple AJAX content loading so this information is really useful.
My comments about styling were more in regards to DOMWindow’s lack of titlebar / close button which was a feature I liked in ThickBox.
Again, thanks for your comments.
March 14th, 2009 at 12:23 am
hi,i’m from china
i want to use this plugin,but it’s has some wrong to call it
urlParams like :
$(‘#boxen_search’).boxen(‘http://www.google.com/search’, {
urlParams: {
showTitleBar: true,
showCloseButton: true,
titleAttribute: ‘title’,
closeButtonText: null,
width: 800,
height: 300,
urlAttribute: ‘href’,
overlayOpacity: 0.8,
modal: true,
postOpen: function() { },
postClose: function() { }
}
});
but it do nothing ,the open frame width and height not changed .
would you pls give me some suggestion ?
thx
Grant
March 14th, 2009 at 9:59 am
urlParams is an object itself. You’ve accidentally placed the other options within it. To use your example, it should look like this
April 3rd, 2009 at 5:21 pm
Superb! but does not work with IE7 & IE8, only with Firefox …
April 5th, 2009 at 10:14 pm
I’m also testing in IE7 and i had problems, but if you play the demo in IE7 it’s all ok … so i guess that is a IE CSS bug
April 6th, 2009 at 6:42 pm
Fix for IE7 :
Check your HEADER “”
It’s OK for me
April 20th, 2009 at 11:36 pm
Hello,
Boxen works great ! Thanks for your work.
Is there a way to get the url called from inside the postClose function ?
Thanks a lot !
April 21st, 2009 at 8:59 am
Sure is. Boxen supports anonymous / global access via the
$.Boxenobject.You can access any option property via the
$.Boxen.optionsmap, eg$.Boxen.options.url. The URL used for the iframe is also available via the internal function$.Boxen.getFullUrl()which includes any urlParams.April 28th, 2009 at 4:48 am
Hi!
Boxen works really great! Very easy to implement.
I have a question: how I can get input fields values from a form opened with boxen?
Best regards,
April 28th, 2009 at 8:39 am
You mean in the parent window? Provided the iframe and parent pages are part of the same domain, just use something like this from the iframe
All that is required is that a method exists on the parent page to handle the data (in my example, the function “myFunction”)
April 28th, 2009 at 7:17 pm
Thank you.
For example “boxen” can be used to open a form to edit something on parent page and “postClose” option can handle the result form and bring the data to parent window (even on hidden fields)?
Will be a nice feature for a next release.
Thank you once again.
All the best
May 24th, 2009 at 9:31 am
I’m trying to load iframe of a google map dynamically, but It shows the same adress through out, my ‘iframed actual’ link shows the correct map, but once its loaded in the lightwindow, Its shows the same map through out.
see http://dal.aquetrans.com/abo-kamal
I’m quite a beginner with google maps and jquery.
Please help
May 24th, 2009 at 9:39 am
I need to open a dynamic google map lightbox.
I will be getting only latitude and longitude from my DB
May 25th, 2009 at 8:38 am
I suggest you read the Google Maps API documentation.
May 25th, 2009 at 8:45 am
It looks like the script at http://dal.aquetrans.com/main/mapof/abo-kamal (your iFrame source) displays something different if there’s a trailing question mark. For example, the direct link is http://dal.aquetrans.com/main/mapof/abo-kamal however the Boxen generated link is http://dal.aquetrans.com/main/mapof/abo-kamal?. Boxen appends the question mark automatically to handle any extra URL parameters.
May 25th, 2009 at 5:29 pm
That was really quick!
Thanx a ton Phil!!!
Didn’t really notice the ‘?’ mark
******SOLVED*********
July 3rd, 2009 at 12:15 am
Hey Phil.
I have it working great. A windows comes up with the google map in it just fine. I wan to incorporate the ability to choose from multiple returned addresses a la http://econym.org.uk/gmap/example_didyoumean1.htm
I edited your code, adding a message div, and the multiple addresses come up fine above the map. But… I can not get the click on the results link to fire back into the open “modal” window. I have used self.parent.place … and many variations – but no luck. Suggestions? Thanks – K
August 26th, 2009 at 6:18 am
Good job!!! But I am looking for a way to close the iframe on click of a button, which is inside the iframe.
August 26th, 2009 at 9:04 am
@Shyamsunder
September 30th, 2009 at 5:08 am
Hey, thanks for the plugin, it is what I was looking for. Just so you know, frameBorder is case sensitive in IE 7 (I know, IE sucks). You may want to capitalize the ‘B’ in the js to make it more browser compatible.
October 8th, 2009 at 10:37 am
Great plugin!
How can I call boxen via regular javascript function? For instance:
function doSomething(){
var t = some useless variable;
boxen(‘http://www.google.com/’);
}
October 8th, 2009 at 11:01 am
@brian If you want to avoid tying Boxen to an anchor or other clickable element, try this (v1.3)
October 8th, 2009 at 11:10 am
@Caleb Thanks. I’ll update the code-base and post an update.
October 8th, 2009 at 3:34 pm
@Phil, Boxen.open() works great. Hats off to you…
November 15th, 2009 at 9:33 am
@Phil, we’ve been using Boxen pretty regularly since October. It’s really an outstanding plugin. An unexpected benefit to an iFrame-based popup was that with a little extra javascript, we realized we could pass virtually any type of information back to the parent window and have whatever page elements refresh. This has greatly simplified things since lots of code can now be self-contained.
Two suggestions for future versions: (1) would be cool to control the opacity color as an option (opacityColor=’#ffffff’). (2) would be nice to have a “spinner” displayed to the user while the iFrame loads the page… then disappear when the page loads. Not sure if Jquery can detect when an iFrame page loads.
Regardless, my compliments on a really USEFUL plugin.
November 15th, 2009 at 1:23 pm
@Brian, you can control the overlay colour using CSS
The spinner is a good idea, I’ll see if I can work out a nice way to display one while the frame contents load.
November 20th, 2009 at 7:57 am
@Phil, my mistake… looks like jquery can access to the iframe load event but only if the iframe source page exists on your domain (javascript security restriction). For this reason, maybe you could have an option called “localSpinner: true” if the developer knows the iframe page will initially be loaded from their domain.
I was suggesting a background option like “opacityColor=#whatever” because we’re calling Boxen.open from different functions on the same page… and each function has a separate look/feel that we control via your options map (height, showTitleBar, etc). I was about to mess around with jquery.css to modify #boxen_overlay dynamically but thought I’d make this suggestion first.
Anyway, these are merely suggestions for an already great plugin… postOpen and postClose are particularly nice features.
November 26th, 2009 at 4:11 am
Nice plugin. It’s really clean and easy to implement.
Is there any way to capture events returned from a different domain inside the iframe, like click events, or the current url inside the iframe?
November 26th, 2009 at 8:25 am
@Jeff No, the same origin policy prevents you from accessing any part of the external resource.
You can access the original URL Boxen loads using
however if the user clicks on a link inside the iframe, you can no longer access the current URL.
November 30th, 2009 at 10:54 am
@Brian I’ve added an “overlayColor” option, just for you
Also added is a really quick example of using a spinner / loading animation. Basically, I just set the background image of the content area to contain the spinner. Once the content loads, provided it sets a background image or colour, the spinner is no longer visible.
December 1st, 2009 at 5:39 pm
@Phil, that’s a really smart way you implemented the spinner. I’m pretty sure the vast majority of websites set a background color these days. The spinner now makes it totally obvious that a page is loading (especially the larger ones).
… and for us, your new overlayColor option is really great. We can now match different modal “skins” with just the right background color and opacity.
A big time “cheers” to you
January 17th, 2010 at 5:12 pm
Jquery plug in is really great thing, thank you for posting this article, especially i like it becouse you showed us all codes, which really helps in these situations when you have to understand what you are doing.
January 20th, 2010 at 7:57 pm
really great a plugin.. very thanx
March 4th, 2010 at 11:10 pm
Hello
i am using this param
$.Boxen.open(”,{
showTitleBar: true,
title: thisTITLE,
titleAttribute: ‘title’,
closeButtonText:’Close Window’,
showCloseButton: true,
width: 950,
height: 540,
url: thisURL,
urlAttribute: null,
overlayOpacity: 0.4,
overlayColor: null,
modal: true,
postOpen: function(contentAreaElement) {},
postClose: function() {
if(PageReload == “Y”){
window.location.reload();
}
}
});
i want to use “Close window” text instead of close button i tried but “Close window” overwrite the close button.
let me suggest.
April 1st, 2010 at 6:14 pm
Phil, quick question…
I’m using Win Firefox 3.6+. When I call the Boxen method using a hard-coded width and height, the canvas (iframe) nicely fades-in to the exact dimensions.
But when I pass the dimensions as function arguments, the canvas is momentarily drawn to about 20×20 pixels, then quickly jumps to the full dimensions. This hesitation is very short but noticeable… almost like a “flicker”.
I’m only getting this w/ Firefox. Both IE and Chrome don’t seem to be effected. I flushed out my cache and opened/closed the browser (Firefox not great w/ memory) but nothing worked.
It’s a strange issue but reproducible in my environment. I was curious if you’ve noticed anything similar… or have any insights.
Otherwise, we use Boxen quite a bit… it’s really one of the most versatile plugins I’ve come across in a while.
April 13th, 2010 at 11:26 pm
Thank you very much for this code.. I have been googling for a few days… thank you.. thank you…
May 14th, 2010 at 8:13 pm
[...] this site I make use of one of the greatest galleries around Pirobox, boxen -to display the menu, and FlippingBook HTML Edition for the menu viewing. You can check the website [...]
June 4th, 2010 at 8:58 pm
Hi!
First of all, nice work!
Just one doubt: is it possible to acurately place the iframe in a page? I mean, i need to have a fixed distance from the top of the page, is it possible? I tried fiddling with the “#boxen_container” in the CSs, but no go..
Can you be of assistance?