<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emanuele Feronato &#187; Javascript</title>
	<atom:link href="http://www.emanueleferonato.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 02 Sep 2010 09:32:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Creation of a Sokoban game with jQuery</title>
		<link>http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/</link>
		<comments>http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 09:55:27 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3028</guid>
		<description><![CDATA[When I said there are games you must be able to make in less than a day, obviously I meant in any language. This is the jQuery version of the Sokoban game I made in less than 2Kb. Or, more precisely, it&#8217;s the javascript version, powered by jQuery. You should know the rules&#8230; click in [...]]]></description>
			<content:encoded><![CDATA[<p>When I said there are games you must be able to <a href="http://www.emanueleferonato.com/2010/05/07/6-games-you-must-be-able-to-make-in-less-than-a-day/">make in less than a day</a>, obviously I meant in <strong>any</strong> language.</p>
<p>This is the jQuery version of the <a href="http://www.emanueleferonato.com/2010/05/19/complete-flash-sokoban-game-in-less-than-2kb/">Sokoban game I made in less than 2Kb</a>.</p>
<p>Or, more precisely, it&#8217;s the javascript version, powered by jQuery.</p>
<p>You should know the rules&#8230; click in the iframe and play using WASD keys.</p>
<p><iframe scrolling="no" frameborder="0" src = "/stuff/jqban/index.html" width = "500" height = "200"></iframe></p>
<p>Let&#8217;s take look at the script:<span id="more-3028"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
	&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
     &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
     &lt;title&gt;jQuery Sokoban by Emanuele Feronato&lt;/title&gt;
     &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
     <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
     $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     	<span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>map.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;wall&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #009900;">&#125;</span>
     		<span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
     	<span style="color: #003366; font-weight: bold;">function</span> possible_move<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> far_tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">3</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">-=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                              left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                              top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                              <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>map.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                             solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                                             <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>
                                   <span style="color: #009900;">&#125;</span>
                              <span style="color: #009900;">&#125;</span>
                              <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>solved<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SOLVED&quot;</span><span style="color: #009900;">&#41;</span>
                              <span style="color: #009900;">&#125;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                         can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>	
               <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>can_move<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>y<span style="color: #339933;">;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>x<span style="color: #339933;">;</span>
     			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#player&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
     				left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                         top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
     			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
               <span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
     	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keydown</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     		<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">65</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">87</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">68</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">83</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     		<span style="color: #009900;">&#125;</span>
        	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
     &lt;style type=&quot;text/css&quot;&gt;
     .wall{
     	background-color:#000000;
     	width:32px;
     	height:32px;
     	position:absolute;
     }
&nbsp;
     .goal{
     	background-color:#ff0000;
     	width:32px;
     	height:32px;
     	position:absolute;	
     }
&nbsp;
     .crate{
     	border:10px solid #00ff00;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
&nbsp;
     #player{
     	border:10px solid #0000ff;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
     &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;sokoban&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><strong>Line 7</strong>: loading jQuery</p>
<p><strong>Line 9</strong>: function to be executed when the document is ready</p>
<p><strong>Lines 10-16</strong>: map initialization. Unfortunately javascript does not support multi-dimensional array creation on the fly&#8230;</p>
<p><strong>Lines 17-46</strong>: drawing the map. As you can see, all map elements are <code>div</code>s of different classes placed according to their position in the map. Movig objects, such as the player and the crates, are stylized with an high <code>z-index</code> to always stay on top of the map. Various objects are mapped this way:</p>
<p>0: empty tile<br />
1: wall<br />
2: place where to drop a crate<br />
3: crate &#8211; crate <code>div</code>s are created with an unique id to indentify them when it&#8217;s time to be pushed by the player<br />
4: player<br />
5: crate on a place where to drop a crate (3+2)<br />
6: player on a place where to drop a crate (4+2)</p>
<p><strong>Lines 47-87</strong>: Core function&#8230; checks for a possible move, eventually moves player and crates and sees if the game is solved.</p>
<p>A game is solved where there are not places where to drop a crate without a crate on them. That is, there isn&#8217;t any <code>2</code> in the map.</p>
<p>Every time I move a crate, I change its <code>id</code> according to its new position.</p>
<p><strong>Lines 88-103</strong>: Checking for key presses.</p>
<p>Hope you enjoy it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creation of a minimalist image gallery in 18 jQuery lines</title>
		<link>http://www.emanueleferonato.com/2010/06/03/creation-of-a-minimalist-image-gallery-in-18-jquery-lines/</link>
		<comments>http://www.emanueleferonato.com/2010/06/03/creation-of-a-minimalist-image-gallery-in-18-jquery-lines/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 10:29:31 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2980</guid>
		<description><![CDATA[The web is full of jQuery galleries, but I think none of them is really minimalist to give people the opportunity to custom and improve it according to their needs, and none of them is fully explained, line by line, style by style, to help jQuery newcomers. So I decided to create the Triqui Gallery, [...]]]></description>
			<content:encoded><![CDATA[<p>The web is full of jQuery galleries, but I think none of them is really minimalist to give people the opportunity to custom and improve it according to their needs, and none of them is fully explained, line by line, style by style, to help jQuery newcomers.</p>
<p>So I decided to create the <a href="http://www.emanueleferonato.com/stuff/triquigallery/"><strong>Triqui Gallery</strong></a>, a simple and fully commented jQuery gallery you can use as a simple gallery or a &#8220;featured photos/games/whatsoever&#8221; slideshow.</p>
<p>According to your comments, I will add more and more functionalities, if this topic receives enough interest.</p>
<p>Let&#8217;s see its main features:</p>
<ul>
<li>All the code in a single page &#8211; no external stylesheet or javascript files to load, to make it easier to understand</li>
<li>Fully commented source code, from styles to jQuery to HTML tags</li>
<li>Compatible with all modern browsers&#8230; at least the latest versions of IE, Firefox, Chrome, Opera and Safari</li>
<li>Easy to configure</li>
<li>Animated captions</li>
<li>Rollover and selected effects on thumbnails</li>
</ul>
<p>You can find all needed information about gallery setup in the HTML file itself, <a href="http://www.emanueleferonato.com/stuff/triquigallery/triquigallery.zip">download it</a> and enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/06/03/creation-of-a-minimalist-image-gallery-in-18-jquery-lines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>17 jQuery powered web games with source code</title>
		<link>http://www.emanueleferonato.com/2010/04/13/17-jquery-powered-web-games-with-source-code/</link>
		<comments>http://www.emanueleferonato.com/2010/04/13/17-jquery-powered-web-games-with-source-code/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 10:07:17 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2704</guid>
		<description><![CDATA[While Flash remains the best software to develop casual web games, jQuery allows you to make interesting things&#8230; nothing to do with pure action Flash games, but in some cases you can get interesting results out of your browser. Here it is a list with 17 jQuery powered games, most of them with clean and [...]]]></description>
			<content:encoded><![CDATA[<p>While Flash remains the best software to develop casual web games, jQuery allows you to make interesting things&#8230; nothing to do with pure action Flash games, but in some cases you can get interesting results out of your browser.</p>
<p>Here it is a list with 17 jQuery powered games, most of them with clean and commented source code ready to download (and before you say it&#8217;s obvious jQuery games have source code, I mean uncompressed, readable source code)</p>
<p><strong>Javascript game engine for jQuery</strong></p>
<p>Not a game but a whole library to develop jQuery games</p>
<p>Main features:</p>
<ul>
<li>multi layer-sprite animations</li>
<li>sprite hierarchies (grouping)</li>
<li>collision detection</li>
<li>swappable sound support</li>
<li>periodic callbacks</li>
<li>keyboard state polling</li>
<li>free and open source license</li>
</ul>
<p><a href="http://gamequery.onaluf.org/" target = "_blank">http://gamequery.onaluf.org/</a> &#8211; Check out the <a href="http://gamequery.onaluf.org/demos.php" target = "_blank">demos</a> too!!</p>
<p><strong>TicTacToe in jQuery</strong></p>
<p>The &#8220;Hello world&#8221; of game programming, now with jQuery</p>
<p><a href="http://thingsilearned.com/2009/06/02/tictactoe-in-jquery/" target = "_blank">http://thingsilearned.com/2009/06/02/tictactoe-in-jquery/</a><span id="more-2704"></span></p>
<p><strong>Tetris with jQuery</strong></p>
<p>The most famous game in the world, </p>
<p><a href="http://fmarcia.info/jquery/tetris/tetris.html" target = "_blank">http://fmarcia.info/jquery/tetris/tetris.html</a></p>
<p><strong>jQuery Mad Libs Word Game</strong></p>
<p>A simple Mad Libs game. There must be something wrong with me because the author says: &#8220;If you did not use Mad Libs as a kid (what is wrong with you&#8230; j/k) it is a fill-in-the-blanks word game where not knowing the word context can lead to some whacky outcomes (and giggling fits)&#8221;.</p>
<p>Yes, I did not use it (do you?)&#8230; that&#8217;s why I hear &#8220;voices&#8221;&#8230;</p>
<p><a href="http://www.bennadel.com/blog/623-jQuery-Demo-Mad-Libs-Word-Game.htm" target = "blank">http://www.bennadel.com/blog/623-jQuery-Demo-Mad-Libs-Word-Game.htm</a></p>
<p><strong>jQuery Mine Sweeper</strong></p>
<p>From the same author of Mad Libs, Mine Sweeper.</p>
<p><a href="http://www.bennadel.com/blog/1737-jQuery-Powered-Mine-Sweeper-Game.htm" target = "_blank">http://www.bennadel.com/blog/1737-jQuery-Powered-Mine-Sweeper-Game.htm</a></p>
<p><strong>A Basic Memory Game with jQuery and PHP</strong></p>
<p>Great and complete tutorial (in Feronato&#8217;s style :)) to create a basic memory game with jQuery and PHP. A must read.</p>
<p><a href="http://www.webdevplayground.com/2009/09/a-basic-memory-game-with-jquery-and-php/" target = "_blank">http://www.webdevplayground.com/2009/09/a-basic-memory-game-with-jquery-and-php/</a></p>
<p><strong>jTypingHero</strong></p>
<p>After Guitar Hero, here it comes Typing hero, a game which will prove your typing skill.</p>
<p><a href="http://www.marcofolio.net/games/jtypinghero_test_your_typing_skills_in_this_jquery_game.html" target = "_blank">http://www.marcofolio.net/games/jtypinghero_test_your_typing_skills_in_this_jquery_game.html</a></p>
<p><strong>jCharacterfall</strong></p>
<p>From the same author, another typing game</p>
<p><a href="http://demo.marcofolio.net/jcharacterfall/" target = "_blank">http://demo.marcofolio.net/jcharacterfall/</a></p>
<p><strong>jQRPG</strong></p>
<p>A very basic &#8211; and incomplete &#8211; Zelda style RPG</p>
<p><a href="http://hokuten.net/2010/jqrpg-%E2%80%94-an-old-school-style-rpg-in-jquery/" target = "_blank">http://hokuten.net/2010/jqrpg-%E2%80%94-an-old-school-style-rpg-in-jquery/</a></p>
<p><strong>Mastermind</strong></p>
<p>jQuery version of the popular brain game</p>
<p><a href="http://www.iohelix.net/mastermind.html" target = "_blank">http://www.iohelix.net/mastermind.html</a></p>
<p><strong>jKill</strong></p>
<p>Point and shoot game, kill presidents and prime ministers</p>
<p><a href="http://www.bulgaria-web-developers.com/projects/javascript/jkill/" target = "_blank">http://www.bulgaria-web-developers.com/projects/javascript/jkill/</a></p>
<p><strong>jQuery Slot Machine</strong></p>
<p>Cute slot machine made with jQuery</p>
<p><a href="http://eyecon.ro/slotmachine/" target = "_blank">http://eyecon.ro/slotmachine/</a></p>
<p><strong>jQuery Pong</strong></p>
<p>it&#8217;s Pong!!</p>
<p><a href="http://blog.benogle.com/2009/04/20/jquery-pong/" target="_blank">http://blog.benogle.com/2009/04/20/jquery-pong/</a></p>
<p><strong>jQuery Sudoku</strong></p>
<p>Really good Sudoku simulator</p>
<p><a href="http://www.anti.ro/games/sudoku/" target = "_blank">http://www.anti.ro/games/sudoku/</a></p>
<p><strong>jQuery Snakey</strong></p>
<p>The famous snake game made with jQuery</p>
<p><a href="http://jquery-snakey.googlecode.com/svn/trunk/index.html" target = "_blank">http://jquery-snakey.googlecode.com/svn/trunk/index.html</a></p>
<p><strong>T&#038;C Surf</strong></p>
<p>Great remake of the old 8-bit Nintendo blockbuster (??)</p>
<p><a href="http://jonraasch.com/blog/jquery-video-game-remake-tc-surf-designs" target = "_blank">http://jonraasch.com/blog/jquery-video-game-remake-tc-surf-designs</a></p>
<p><strong>Tunnel game</strong></p>
<p>Another classic, fly with your spaceship inside a tunnel, don&#8217;t touch its borders</p>
<p><a href="http://arcade.christianmontoya.com/tunnel/" target = "_blank">http://arcade.christianmontoya.com/tunnel/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/13/17-jquery-powered-web-games-with-source-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Loading WP posts with Ajax and jQuery &#8211; a real world example</title>
		<link>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/</link>
		<comments>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 21:28:26 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2677</guid>
		<description><![CDATA[If you are looking for a real world example made with Loading WordPress posts with Ajax and jQuery or you haven&#8217;t played LineBall yet, or you played it, you loved it and you want to know who made the music, this is the right blog post. I am finishing and polishing LineBall&#8217;s official site built [...]]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a real world example made with <a href="http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/">Loading WordPress posts with Ajax and jQuery</a> or you haven&#8217;t played <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a> yet, or you played it, you loved it and you want to know who made the music, this is the right blog post.</p>
<p>I am finishing and polishing <a href="http://www.lineball-game.com/" target = "_blank">LineBall&#8217;s official site</a> built with WordPress and Ajax.</p>
<p>I plan to finish it before this week ends, then release the theme next week. So if you have suggestions, feedback or want more information, this is your turn.</p>
<p>Help me to make the ultimate WordPress Flash game theme.</p>
<p>Some features already developed:</p>
<p>* Highly customizable by setup page &#8211; no need to edit the code</p>
<p>* Ajax powered to browse it without reloading the header (the game itself)</p>
<p>* Minimal theme, a very few lines of code</p>
<p>Let me know what do you think about it. And don&#8217;t pay that much attention to contents&#8230; I just wrote a couple of lines just to make some tests</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loading WordPress posts with Ajax and jQuery</title>
		<link>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/</link>
		<comments>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 21:52:41 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2650</guid>
		<description><![CDATA[After seeing how to include jQuery Ajax calls in your WordPress blog, it&#8217;s time to load posts on the fly, without reloading the page. As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed. Look how I load the posts under the header by clicking on their titles&#8230; even [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">how to include jQuery Ajax calls in your WordPress blog</a>, it&#8217;s time to load posts on the fly, without reloading the page.</p>
<p>As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed.</p>
<p>Look how I load the posts under the header by clicking on their titles&#8230; even the ones with &#8220;more&#8221; tag appear complete without reloading the page:</p>
<p><object width="520" height="417"><param name="movie" value="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="417"></embed></object></p>
<p>Let&#8217;s start with <code>header.php</code> modifications</p>
<p>This is the script I added under</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_head<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That is the JQuery part<span id="more-2650"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;h2 a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> post_id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #009900;">&#41;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loading...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://&lt;?php echo $_SERVER[HTTP_HOST]; ?&gt;/triqui-ajax/&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>post_id<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>I already explained the first 4 lines in the <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">previous tutorial</a>.</p>
<p><strong>Line 5</strong>: Waiting for the user to click on an hypertext over an <code>h2</code> heading. I am doing it because in Kubrick post titles are rendered this way:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h2&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;....</pre></div></div>

<p>This should change from theme to theme, but this works on Kubrick</p>
<p><strong>Line 6</strong>: Retrieving the <code>rel</code> attribute of the link and saving it in a variable called <code>post_id</code>. You&#8217;ll see later in this tutorial how this attribute will contain the unique ID of the post we want to show.</p>
<p><strong>Line 7</strong>: In an element with <code>your_post_here</code> id I am writing &#8220;loading&#8230;&#8221; because I&#8217;m starting to load the post. You&#8217;ll see later in this tutorial where to place the element</p>
<p><strong>Line 8</strong>: Now thw ajax magic&#8230; in the <code>your_post_here</code> element this time I load the output of a page of the blog called <code>triqui-ajax</code>. You&#8217;ll se later in this tutorial how to create it. This will work if the permalinks of your blog aren&#8217;t the default ones. Go to <code>Settings</code> -> <code>Permalinks</code> and select <code>Day and name</code></p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax_2.jpg" alt="" /></p>
<p>I am also passing in <code>POST</code> a variable called <code>id</code> with the content of <code>post_id</code> variable (the post unique id)</p>
<p><strong>Line 9</strong>: I added this line to prevent the browser to jump to the link&#8230; remember? I want to load the post in the same page.</p>
<p>Now it&#8217;s time to make some changes to <code>index.php</code></p>
<p>This is how I modified it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * @package WordPress
 * @subpackage Default_Theme
 */</span>
&nbsp;
get_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;div id=&quot;content&quot; class=&quot;narrowcolumn&quot; role=&quot;main&quot;&gt;
	&lt;div id = &quot;your_post_here&quot;&gt;&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
			&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
				&lt;h2&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; title=&quot;Permanent Link to <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title_attribute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/h2&gt;
				&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
				&lt;div class=&quot;entry&quot;&gt; 
					<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;/div&gt;
&nbsp;
				&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
			&lt;/div&gt;
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;div class=&quot;navigation&quot;&gt;
			&lt;div class=&quot;alignleft&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> next_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;laquo; Older Entries'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
			&lt;div class=&quot;alignright&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> previous_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Newer Entries &amp;raquo;'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		&lt;/div&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;h2 class=&quot;center&quot;&gt;Not Found&lt;/h2&gt;
		&lt;p class=&quot;center&quot;&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_search_form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_sidebar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>To tell the truth, I only added a line and changed a bit another one:</p>
<p><strong>Line 10</strong>: This is the div that will contain the post, the one I change with jQuery (remember? <strong>lines 7</strong> and <strong>8</strong> in the jQuery script)</p>
<p><strong>Line 16</strong>: Here I changed the <code>rel</code> attribute to store the unique id of the post I want to load. I used it at <strong>line 6</strong> in the jQuery script</p>
<p>And the template files do not need any other change.</p>
<p>Now it&#8217;s time to create a new page template with some code that will load the selected post.</p>
<p>In your template directory create a new php file, no matter its name&#8230; you can call it <code>example.php</code> and write this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Template Name: Triqui Ajax Post
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> get_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
		&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h2&gt;
		&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
		&lt;div class=&quot;entry&quot;&gt; 
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/div&gt;
&nbsp;
		&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
	&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see it&#8217;s almost the same code you can find in <code>index.php</code> at <strong>lines 15-24</strong>&#8230; because I just want to load a post in the same way the index file does. I just removed the anchor tag in the title, because I don&#8217;t want it to be clickable. And obviously I read the <code>id</code> value passed by the jQuery script at <strong>line 8</strong></p>
<p>The only interesting lines are <strong>lines 2-4</strong>&#8230; yes, the comment&#8230; because this way I am giving the template page the name <code>Triqui Ajax Post</code>.</p>
<p>Now in your admin area create a new page, call it <code>Triqui Ajax</code> (do you remember the permalink url at <strong>line 8</strong> in the jQuery script&#8230;) and select <code>Triqui Ajax Post</code> as template</p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax.jpg" alt="" /></p>
<p>And that&#8217;s it&#8230; you don&#8217;t need anything else, and the blog will work as in the video.</p>
<p>Enjoy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Including jQuery Ajax calls in your WordPress blog</title>
		<link>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/</link>
		<comments>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 15:33:46 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2637</guid>
		<description><![CDATA[I want to show you how to include jQuery ajax calls in your WordPress blog. The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page. This is useful when you want the user to interact with your blog without forcing him to reload the [...]]]></description>
			<content:encoded><![CDATA[<p>I want to show you how to include jQuery ajax calls in your WordPress blog.</p>
<p>The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page.</p>
<p>This is useful when you want the user to interact with your blog without forcing him to reload the page. Or when you want some events to happen &#8220;automatically&#8221; without user interaction.</p>
<p>In this video I am changing the header at every second without reloading the page.</p>
<p><object width="520" height="420"><param name="movie" value="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="420"></embed></object></p>
<p>Also notice I am doing it from the default Kubrick theme with a fresh installation&#8230; I am not using any jQuery powered theme or plugin. Let&#8217;s see how can you make it.<span id="more-2637"></span></p>
<p>In this case all changes have been made in <code>header.php</code> file, adding nine lines after</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_head<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here they are:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> refresh <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>   	
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#description_span&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php bloginfo('template_url'); ?&gt;/example.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>First, I am loading jQuery using Google as content delivery network.</p>
<p>Then, when the document is ready, I disable the cache to stop jQuery <code>.load</code> response from being cached.</p>
<p>Now I just execute an ajax call to <code>example.php</code> file at every second and print the result in the element with <code>description_span</code> id</p>
<p>This is <code>example.php</code> content</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now it&#8217;s time to locate <code>description_span</code> element&#8230; it&#8217;s in the header, next to the description of the blog.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;description&quot;&gt;&lt;?php bloginfo('description'); ?&gt; like other &lt;span id = &quot;description_span&quot;&gt;&lt;/span&gt; ones&lt;/div&gt;</pre></div></div>

<p>And that&#8217;s it. This time I changed the header without touching the content, next time (the aim of my experiment) I&#8217;ll change the content without touching the header, maybe because the header contains a Flash game called <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a>&#8230; (yes, I am designing a WP theme for a game official site).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Create a dynamic content animated footer ad for your site in just 9 jQuery lines &#8211; 17 lines version</title>
		<link>http://www.emanueleferonato.com/2010/03/10/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-9-jquery-lines-17-lines-version/</link>
		<comments>http://www.emanueleferonato.com/2010/03/10/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-9-jquery-lines-17-lines-version/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 17:35:09 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2510</guid>
		<description><![CDATA[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&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>Some days ago I showed you how to <a href="http://www.emanueleferonato.com/2010/03/03/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-15-jquery-lines/">create a dynamic content animated footer ad for your site in just 9 jQuery lines</a>.</p>
<p>Now it&#8217;s time to write eight more lines to add two important features:</p>
<p>The first, as suggested in a comment, is allowing to set a delay before the ad appears.</p>
<p>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&#8217;t appear again, until you close the browser window and open it again.</p>
<p><a href="http://www.emanueleferonato.com/stuff/triqui_ad/index2.html" target = "_blank">This is what we&#8217;ll get</a>&#8230; the ad appears after three seconds, and only once in a session.</p>
<p>Now let&#8217;s see the new lines added:<span id="more-2510"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$.<span style="color: #660066;">delay</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>time<span style="color: #339933;">,</span>ret_function<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>timer<span style="color: #339933;">:</span>setTimeout<span style="color: #009900;">&#40;</span>ret_function<span style="color: #339933;">,</span>time<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> delayed_start <span style="color: #339933;">=</span> <span style="color: #CC0000;">3000</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> path_to_dynamic_file <span style="color: #339933;">=</span> <span style="color: #3366CC;">'ajax.php'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;triqui_ad&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		document.<span style="color: #660066;">cookie</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;triqui_ad=1&quot;</span><span style="color: #339933;">;</span>
		$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">wrap</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;triqui_container&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> delayed_start <span style="color: #339933;">=</span> $.<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span>delayed_start<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span>path_to_dynamic_file<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &amp;bull; &lt;a id=&quot;triqui_ad_close&quot;&gt;Close&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad_close'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
		   			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		  		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Lines 3-6</strong>: Declaring a new jQuery function called <code>delay</code>, that&#8217;s just a callback after a given amount of milliseconds. </p>
<p><strong>Line 8</strong>: Variable storing the number of milliseconds to wait before showing the ad</p>
<p><strong>Line 9</strong>: Variable storing the path to the dynamic file</p>
<p><strong>Line 10</strong>: Checking if I have a cookie called <code>triqui_ad</code>. The statement is true if I don&#8217;t have it.</p>
<p><strong>Line 11</strong>: Creating a cookie called <code>triqui_ad</code>. Since there is not an expiration date, it will expire when the user will close the browser.</p>
<p><strong>Line 14</strong>: The entire function explained in <a href="http://www.emanueleferonato.com/2010/03/03/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-15-jquery-lines/">part 1</a> is executed only after the preset delay.</p>
<p>And that&#8217;s it&#8230; any idea for additional features?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/10/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-9-jquery-lines-17-lines-version/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Create a dynamic content animated footer ad for your site in just 9 jQuery lines</title>
		<link>http://www.emanueleferonato.com/2010/03/03/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-15-jquery-lines/</link>
		<comments>http://www.emanueleferonato.com/2010/03/03/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-15-jquery-lines/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 21:50:49 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2460</guid>
		<description><![CDATA[Have you ever seen those ads sliding up from the bottom of your page when you are visiting a website? We are going to create a simple one in just 9 jQuery lines. Moreover, with this script you will need to add just one line to your site to display the ad. Just to make [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever seen those ads sliding up from the bottom of your page when you are visiting a website? We are going to create a simple one in just 9 <a href="http://jquery.com/" target  "_blank">jQuery</a> lines.</p>
<p>Moreover, with this script you will need to add just one line to your site to display the ad.</p>
<p>Just to make it easier to understand, I am showing you for free the principles some &#8220;companies&#8221; <a href="http://www.google.com/webhp?hl=en#hl=en&#038;source=hp&#038;q=footer+ad&#038;rlz=1R2GZAZ_enIT351&#038;aq=f&#038;aqi=g10&#038;aql=&#038;oq=&#038;fp=f15a301262521d7" target = "_blank">sell at more than $40</a>. According to such sites, ads like the one we are about to make should increase your conversions, have more readers sign up to your newsletter, and make you live in harmony. All in one-</p>
<p>Unfortunately, I am only showing you how to create such ad in a bunch of jQuery lines.</p>
<p>This is what we are going to make: <a href="http://www.emanueleferonato.com/stuff/triqui_ad/" target = "_blank">Watch the example</a>.</p>
<p>Ready for the recipe? Here we go:</p>
<p><strong>A web page</strong></p>
<p>Obviously, you need a webpage. Any webpage will fit, as long as you include</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;triqui_ad&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>just before the <code>&lt;/body&gt;</code> tag.</p>
<p><strong>jQuery</strong></p>
<p>Then it&#8217;s time to add the power of jQuery to the page: insert this script between <code>&lt;head&gt;</code> and <code>&lt;/head&gt;</code><span id="more-2460"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">wrap</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;triqui_container&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajax.php'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &amp;bull; &lt;a id=&quot;triqui_ad_close&quot;&gt;Close&lt;/a&gt; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_ad_close'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
   			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#triqui_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Line 1</strong>: loading the latest jQuery version. I am loading it directly from Google to get the latest stable version with no stress for my server.</p>
<p><strong>Line 3</strong>: Function to be executed when the document DOM is fully loaded.</p>
<p><strong>Line 4</strong>: Disabling the cache when using Ajax calls. That&#8217;s why when you reload the page in the <a href="http://www.emanueleferonato.com/stuff/triqui_ad/" target = "_blank">example</a>, you always get the current timestamp</p>
<p><strong>Line 5</strong>: I had to add this line because I wanted the final user to type in as less code as possible, so at the moment you just have to add</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;triqui_ad&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>while the &#8220;real&#8221; code should be</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id = &quot;triqui_container&quot;&gt;&lt;div id=&quot;triqui_ad&quot;&gt;&lt;/div&gt;&lt;/div&gt;</pre></div></div>

<p>Using <code>wrap</code> you will (guess what?) wrap an HTML structure around the selected element.</p>
<p><strong>Line 6</strong>: This is the most important line. I am loading the result of the Ajax call made on the <code>ajax.php</code> file. This means I am printing inside the <code>div</code> everything <code>ajax.php</code> will echo. In my case, it will print the current date since this is the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Hello, I am a dynamic content created on &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'l jS \of F Y h:i:s A'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can imagine, <code>load</code> loads data from the server and place the returned HTML into the matched element. The only issue is it caches the result, that&#8217;s why I disabled it at <strong>line 4</strong></p>
<p><strong>Line 7</strong>: Here I am showing the content of the div containing the ad. When you&#8217;ll look at the css, you&#8217;ll find there was a <code>display:none</code> to keep it hidden until the script is ready to work.</p>
<p><strong>Line 8</strong>: Time to slide in the ad</p>
<p><strong>Line 9</strong>: Appending a link to close the ad</p>
<p><strong>Line 10</strong>: Triggering the link used to close the ad</p>
<p><strong>Line 11</strong>: Sliding away the ad when the user clicks on the link</p>
<p><strong>CSS</strong></p>
<p>And finally it&#8217;s time to stylize the ad.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#triqui_container</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#triqui_ad</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#e5e5e5</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#triqui_ad_close</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">pointer</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">underline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>You can custom the stylesheet as you want as long as you set <code>display:none</code> to the ad and <code>position:fixed</code> and <code>bottom:0</code> to the container.</p>
<p>Have fun&#8230; would you like more options?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/03/create-a-dynamic-content-animated-footer-ad-for-your-site-in-just-15-jquery-lines/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Placing ads before a game loads with jQuery</title>
		<link>http://www.emanueleferonato.com/2009/12/30/placing-ads-before-a-game-loads-with-jquery/</link>
		<comments>http://www.emanueleferonato.com/2009/12/30/placing-ads-before-a-game-loads-with-jquery/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 11:50:08 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Monetize]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2143</guid>
		<description><![CDATA[When you run a Flash game portal or a Flash game official site, you normally place ads everywhere hoping someone will notice it. Obviously the most interesting place where to place ads is already taken by the game itself, so we are going to create a javascript preloader that will show an ad for 15 [...]]]></description>
			<content:encoded><![CDATA[<p>When you run a Flash game portal or a Flash game official site, you normally place ads everywhere hoping someone will notice it.</p>
<p>Obviously the most interesting place where to place ads is already taken by the game itself, so we are going to create a javascript preloader that will show an ad for 15 seconds before the game starts loading.</p>
<p>In order to do it, you need <a target = "_blank" href="http://jquery.com/">jQuery</a> and <a href="http://code.google.com/p/swfobject/" target ="_blank">swfobject</a></p>
<p>Now let&#8217;s suppose the game has to be embedded in a <code>div</code> called <code>the_game</code>, just like I did in my <a target = "_blank" href="http://www.barbalance-game.com/">BarBalance official site</a>.</p>
<p>Just create the <code>div</code> this way:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id = &quot;the_game&quot;&gt;
	&lt;h1&gt;BarBalance game is loading&lt;/h1&gt;
	&lt;SCRIPT language=&quot;Javascript&quot;&gt;
	var cpmstar_rnd=Math.round(Math.random()*999999);
	var cpmstar_pid=10016;
	document.writeln(&quot;&lt;SCR&quot;+&quot;IPT language='Javascript' src='http://server.cpmstar.com/view.aspx?poolid=&quot;+cpmstar_pid+&quot;&amp;script=1&amp;rnd=&quot;+cpmstar_rnd+&quot;'&gt;&lt;/SCR&quot;+&quot;IPT&gt;&quot;);
	&lt;/SCRIPT&gt;
&lt;/div&gt;</pre></div></div>

<p>I used some SEO like an <code>h1</code> tag and relevant content, and a <a href="http://www.cpmstar.com/" target = "_blank">CPMStar</a> because it&#8217;s animated and we suppose won&#8217;t bore the player during the 15 seconds.</p>
<p>If you don&#8217;t have a CPMStar account, feel free to use my code :)</p>
<p>Then the jQuery part:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
	$(document).ready(function(){
		setTimeout(function(){
			$(&quot;#the_game&quot;).fadeOut(2000,function(){
				swfobject.embedSWF(&quot;barbalance.swf&quot;, &quot;the_game&quot;, &quot;640&quot;, &quot;480&quot;, &quot;9.0.0&quot;);
			}); 
		},15000);
	}); 
&lt;/script&gt;</pre></div></div>

<p>It&#8217;s just a 15000 milliseconds timeout fading out with a callback replacing the existing content with the Flash game.</p>
<p>Look at  <a target = "_blank" href="http://www.barbalance-game.com/">BarBalance official site</a> to see it working.</p>
<p>I can&#8217;t wait to know this ad eCPM&#8230; did you already use something similar?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2009/12/30/placing-ads-before-a-game-loads-with-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JQuery powered lights off effect</title>
		<link>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/</link>
		<comments>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 14:58:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=1735</guid>
		<description><![CDATA[A couple of days ago a fan of my Facebook page asked me for a script to make the effect you see on this page. Obviously simply reverse-engineering the script wouldn&#8217;t be enough for me, so I decided to create a lights off effect that make any content in a given div remain highlighted while [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago a fan of <a target = "_blank" href="http://www.facebook.com/pages/Emanuele-Feronato/50374255154">my Facebook page</a> asked me for a script to make the effect you see on <a href="http://test.jackread.co.uk/LightsOff/lightsoff.htm" target = "_blank">this page</a>.</p>
<p>Obviously simply reverse-engineering the script wouldn&#8217;t be enough for me, so I decided to create a lights off effect that make <strong>any</strong> content in a given <code>div</code> remain highlighted while the rest of the page fades to black (or to any color).</p>
<p>I used <a target = "_blank" href="http://jquery.com/">JQuery</a> to manage the fade effect because it&#8217;s the best Javascript library available at the moment.</p>
<p>As a developer, I also tried MooTools and scriptaculous but believe me <a target = "_blank" href="http://jquery.com/">JQuery</a> is some steps ahead.</p>
<p>Now let&#8217;s see this script:<span id="more-1735"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
     &lt;head&gt;
          &lt;title&gt;Emanuele Feronato's lights off effect&lt;/title&gt;
          &lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
          &lt;script&gt;
               $(document).ready(function(){
                    $(&quot;#the_lights&quot;).fadeTo(1,0);
                    $(&quot;#turnoff&quot;).click(function () {
                         $(&quot;#the_lights&quot;).css({'display' : 'block'});
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,1);
                    });
                    $(&quot;#soft&quot;).click(function () {
                         document.getElementById(&quot;the_lights&quot;).style.display=&quot;block&quot;;
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,0.8);
                    });
                    $(&quot;#turnon&quot;).click(function () {
                         document.getElementById(&quot;the_lights&quot;).style.display=&quot;block&quot;;
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,0);
                    });
               });
          &lt;/script&gt;
          &lt;style&gt;
               html{
                    width:100%;
                    height:100%;
                    margin:0px;
               }
               #the_lights{
	              background-color:#000;
	              height:100%;
	              width:100%;
	              position:absolute;
	              top:0;
	              left:0;
	              display:none;
               }
               #standout{
                    padding:5px;
                    background-color:white;
                    position:relative;
                    z-index:1000;
               }
          &lt;/style&gt;
     &lt;/head&gt;
&lt;body&gt;
     &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas scelerisque libero euismod nulla porttitor egestas. Nulla fermentum facilisis sagittis. Vestibulum commodo pretium diam, vitae scelerisque lectus bibendum nec. Curabitur a metus est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed nunc dolor, pretium eget venenatis ut, condimentum at sem. Vivamus rhoncus ullamcorper turpis. Aliquam sit amet risus diam, vel mattis est. Quisque sodales eros id nulla rhoncus posuere. Nullam accumsan lorem quis dolor tempor congue. Vivamus ligula augue, commodo malesuada tempor non, tristique id enim. Sed dapibus sagittis porttitor. Suspendisse faucibus scelerisque eros, ac hendrerit orci scelerisque sit amet.&lt;/p&gt;
     &lt;div id = &quot;standout&quot;&gt;&lt;p&gt;Everything inside this div will remain highlighted&lt;/p&gt;&lt;div id = &quot;turnoff&quot;&gt;Lights off&lt;/div&gt;&lt;div id = &quot;soft&quot;&gt;Soft lights&lt;/div&gt;&lt;div id = &quot;turnon&quot;&gt;Lights on&lt;/div&gt;&lt;/div&gt;
     &lt;p&gt;Vestibulum interdum, odio ut congue imperdiet, justo ligula bibendum nisl, ac placerat orci eros nec tortor. Maecenas rhoncus felis vitae elit consectetur vitae mattis metus tincidunt. Donec nec ipsum quis tellus posuere ultricies. Ut tempus tortor nec sem blandit ut facilisis nisl scelerisque. Nulla venenatis ante sit amet nisi dictum varius. Etiam congue dui eu diam fermentum et tincidunt magna egestas. Sed tincidunt magna et metus molestie malesuada. Nam tempus dignissim porta. Vivamus iaculis, orci hendrerit condimentum viverra, eros nisl euismod arcu, nec pulvinar tellus elit eu nulla. Vestibulum mattis, ligula sit amet euismod commodo, augue lorem fermentum magna, et rhoncus nisl urna nec purus. Suspendisse potenti. Fusce fermentum orci at orci posuere ornare a imperdiet justo. In hac habitasse platea dictumst. Duis adipiscing leo nec risus varius auctor. Cras sed erat massa, quis egestas leo. Vivamus dignissim lacinia leo, ac interdum nisi facilisis eget.&lt;/p&gt;
     &lt;div id=&quot;the_lights&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>The <code>div</code> is the one called <code>standout</code> at <strong>line 48</strong> and it has nothing special but a background color (it can&#8217;t be transparent) and a z-index along with a relative position (<strong>lines 40-42</strong>).</p>
<p>Obviously the z-index should be higher than the rest of the elements.</p>
<p>The other interesting <code>div</code> is <code>the_lights</code> (<strong>line 50</strong>), a black div covering all the page, initially invisible (<strong>lines 30-36</strong>). Playing with its transparency will simulate the lights effect</p>
<p>Now it&#8217;s all a matter of JQuery:</p>
<p><strong>Line 7</strong>: Listening for the document to be ready</p>
<p><strong>Line 8</strong>: Once it&#8217;s ready, the first thing is setting <code>the_lights</code> alpha to zero. I don&#8217;t set it from CSS but use <code>fadeTo</code> because I need to initialize the fading process. The entire lights out effect is just playing with <code>the_lights</code> and some tweening.</p>
<p><strong>Line 9</strong>: Here I am listening for the <code>turnoff</code> div to be clicked</p>
<p><strong>Line 10</strong>: Showing <code>the_lights</code> div&#8230; this is the same thing as<br />
<code>document.getElementById('the_lights').style.display='block'</code></p>
<p><strong>Line 11</strong>: Fading the black div to full opacity.</p>
<p>This will give the &#8220;lights out effect&#8221;</p>
<p>Try the <a href="/stuff/lightsoff/" target = "_blank">example at this page</a> will show you the effect&#8230; click on &#8220;lights on&#8221;, &#8220;lights off&#8221; or &#8220;soft lights&#8221; and see what happens.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
