Creating beautiful pop-ups using jquery
When it comes to pop-up windows, people don’t really associate them to being accessible and they are some what annoying. But sometimes it is necessary for whatever reason.
If you think about it, a link that opens up a new window is one of the most simple thing you can do with javascript & HTML. There is no reason for it to be ugly.
This is the old fashion and totally wrong way of doing it:
<a href="javascript:window.open(http://www.popup.com/);">Popup link</a>
Why? The a tag is not linking to a link but instead it is running some javscript. That means the whole meaning of the link is gone.
Here is what an accessible and clean link looks like:
<a href="http://www.popup.com" rel="pop-up">Open a Popup</a>
In this version, there is no inline javascript which is always a good pratice to separate HTML and javascript. It keeps everything simple and clean. The addition of the rel attribute is used to describe the relationship between the current page and the href destination of the anchor.
Now we have this beautiful HTML link. It is time to do some magic with javascript and jQuery.
( function popup() { $( document ).ready( function() { $("a[rel='pop-up']").click(function () { var features = "height=700,width=800,scrollTo,resizable=1,scrollbars=1,location=0"; newwindow=window.open(this.href, 'Popup', features); return false; }); } ); }() );
This is a very simple javascript using jQuery. Basically, the code is looking for all the links with the rel attribute and assign a onclick event to open the pop-up window. Of course, there are different ways to abstract the above code to make it more flexible. But the idea is to have beautiful pop-up link and have the javascript to do the work later.
Comments so far
doesn’t work in IE
So much work for no demo, really no demo????
Working fine in all browser..thanks…
Thanks for this example Richard, your a life saver, no working on putting a file manager style interface in the popup and configuring the javascript to work with the page the pop up came from.
this popup window i don’t like
Wheres the freakin demo dude? NEWB.
Its working fine though there is some problem in IE older versions. However would like to view some demo and also it would have been gr8 if the popup was bit stylish
its a just a link
I LOVE IT. Just the answer I needed and no tweaking.
That is really amazing code. I will use it for my admin panel
Why would you ever need a demo? It’s a popup! You’re a NEWB if you don’t know what a popup looks like!
Post a comment
Trackbacks/Pingbacks