Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version

Some days ago I showed you how to create a dynamic content animated footer ad for your site in just 9 jQuery lines.

Now it’s time to write eight more lines to add two important features:

The first, as suggested in a comment, is allowing to set a delay before the ad appears.

The second is using a cookie to let the ad appear only once in a session. This means if you refresh the page, the ad won’t appear again, until you close the browser window and open it again.

This is what we’ll get… the ad appears after three seconds, and only once in a session.

Now let’s see the new lines added:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$.delay = function(time,ret_function){
	var ret = {timer:setTimeout(ret_function,time)}
	return ret;
};
$(document).ready(function(){
	var delayed_start = 3000;
	var path_to_dynamic_file = 'ajax.php';
	if(document.cookie.indexOf("triqui_ad")==-1){
		document.cookie="triqui_ad=1";
		$.ajaxSetup({cache:false});
		$('#triqui_ad').wrap('<div id="triqui_container"></div>');
		var delayed_start = $.delay(delayed_start,function(){
			$('#triqui_ad').load(path_to_dynamic_file,function(){
				$('#triqui_ad').css('display','block');
				$('#triqui_container').hide().slideDown('slow');
				$('#triqui_ad').append(' &bull; <a id="triqui_ad_close">Close</a>');
				$('#triqui_ad_close').click(function(){ 
		   			$('#triqui_container').slideUp('slow'); 
		  		}); 
			});
		});
	} 
});
</script>

Lines 3-6: Declaring a new jQuery function called delay, that’s just a callback after a given amount of milliseconds.

Line 8: Variable storing the number of milliseconds to wait before showing the ad

Line 9: Variable storing the path to the dynamic file

Line 10: Checking if I have a cookie called triqui_ad. The statement is true if I don’t have it.

Line 11: Creating a cookie called triqui_ad. Since there is not an expiration date, it will expire when the user will close the browser.

Line 14: The entire function explained in part 1 is executed only after the preset delay.

And that’s it… any idea for additional features?

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 5 comments

  1. dario111cro

    on March 10, 2010 at 8:38 pm

    Nice and simple. I like it :)

  2. Kalma

    on March 30, 2010 at 12:24 pm

    Thank you. Nice.

  3. Marco Lee

    on August 6, 2010 at 2:05 pm

    This is actually nice. I just can’t seem to make this work in firefox. Chrome shows it though

  4. jewel

    on October 18, 2010 at 12:28 pm

    Thank for nice post. Could you please tell me how to make it outo close after given certain time. Thanks

  5. James

    on November 3, 2010 at 10:21 pm

    Really awesome and simple, i almost bought een footer ad WP plugin . thanks for your footer ad script!