Create a one-time popup using jQuery
Thought I would go through the steps of creating a one-time popup for a webpage using jQuery. The goal is to create a styled popup that is shown to visitors a single time when they visit a webpage, but not shown on future visits to that pages.
What we will need:
With only few lines of code added the head of a page we can make it ready to handle our popup.
Add into the Head area (between the HEAD tags):
What the above does is first make reference to all the components we require to get the One-time popup to work.
The script section:
Adding the popup:
This is the hidden content which will be displayed to the visitor as a popup. This can be any HTML code and can contain tables, forms or any styling you want and its location can be anywhere in a page between the BODY tags.
Wrap up:
You can see this is a very trivial task when you know the right components to use. With jQuery it couldn't be easier.
What we will need:
- jQuery: Provides the JavaScript framework for the code and does a lot of the heavy lifting
- jQuery Thickbox: Provides the actual popup and gives us an amazing amount of control on whats shown
- jQuery Cookie: Provides the mechanism to track users coming back to the page to prevent them seeing the popup more than once
With only few lines of code added the head of a page we can make it ready to handle our popup.
Add into the Head area (between the HEAD tags):
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.cookie.js"></script>
<script type="text/javascript" src="scripts/thickbox/thickbox.js"></script>
<link rel="stylesheet" type="text/css" href="scripts/thickbox/thickbox.css">
<script type="text/javascript">
$(document).ready(function(){
if ($.cookie('20080521') != '1') {
tb_show("<strong>We're Moving!</strong>",
"#TB_inline?height=190&width=275&inlineId=mypopup","");
$.cookie('20080521', '1', { expires: 20 });
}
});
</script>
What the above does is first make reference to all the components we require to get the One-time popup to work.
The script section:
- Checks if the document (page) has fully loaded
- When its loaded it looks for the cookie that is set when the user has viewed the popup. What I did was date code the cookie so you can easily force a popup just by changing the cookie's name
- If the cookie has not be set then a popup is displayed to the user and the cookie gets set with a 20 day expiration
- Each subsequent visit to the page will not trigger the popup because the cookie has been set
Adding the popup:
<div id="mypopup" style="display:none">
<p id="msg">
This is a sample popup.
</p>
</div>
This is the hidden content which will be displayed to the visitor as a popup. This can be any HTML code and can contain tables, forms or any styling you want and its location can be anywhere in a page between the BODY tags.
Wrap up:
You can see this is a very trivial task when you know the right components to use. With jQuery it couldn't be easier.
Labels: development, javascript, jquery






11 Comments:
Nice work. Thanks for sharing.
Do you have a way of delaying the popup?
By
John Fox, at 3:21 PM EST
Forget my earlier post. Figured it out.
One thing I don't see is a way to make the cookies work for the entire site, so a visitor sees the cookie only once per session, rather than on every page.
By
John Fox, at 7:06 AM EST
Not sure what you are attempting. The code is for a single page looking for a specific cookie. Why can't you just use the same cookie name. If the cookie name is the same no cookie will be set.
Maybe I am misunderstanding.
By
Veign, at 8:13 AM EST
Hi - Thanks for this script. Very handy. I'm trying to delay the pop up as well. Could you point me in the right direction?
Thanks very much!
By
Anonymous, at 11:54 PM EST
Very nice indeed is it possible to change the text area size or the size of the popup window it self I have liiked through the css and cant see how to achieve this
By
Anonymous, at 12:43 PM EST
Look at the tb_show line. It controls the size of the popup. The style would be controlled by the styling on the container you're popping up.
By
Veign, at 1:26 PM EST
Thanks for the pointer I took a look in the css but there is no reference to tb_show I did find it it in thickbox.js changing this made no difference
$("body","html").css({height: "100%", width: "100%"}); I think this is relating to the overlay as I a newbie to js I may be wrong so any help be appreciated thanks in advance
By
Anonymous, at 2:53 PM EST
Its in the tb_show in the header:
tb_show("We're Moving!","#TB_inline?height=190&width=275&inlineId=mypopup","");
See the height and width defined?
By
Veign, at 3:05 PM EST
Ah thanks what can I say totally missed that again thank you I am sure this will come to be very useful :)
By
Anonymous, at 3:14 PM EST
It would be great to have a demo that works in all browsers.
Thanks
By
Anonymous, at 10:24 AM EST
What browser doesn't it work in?
By
Veign, at 10:28 AM EST
Post a Comment
Links to this post:
Create a Link
<< Home