<?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>Bramme.net &#187; Code</title>
	<atom:link href="http://www.bramme.net/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bramme.net</link>
	<description>The portfolio of Bram Van der Sype</description>
	<lastBuildDate>Mon, 10 May 2010 12:19:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Addition to jQuery slider</title>
		<link>http://www.bramme.net/2008/09/addition-to-jquery-slider/</link>
		<comments>http://www.bramme.net/2008/09/addition-to-jquery-slider/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 08:01:22 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.bramme.net/?p=95</guid>
		<description><![CDATA[A few days ago I posted a script that allowed you to create a jQuery slideToggle that would remember it&#8217;s last state throughout page reloads. The original idea came from Matt over at ifoh designs. He even was so kind to comment on my version. He mentioned something though I think in return is a [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I posted a script that allowed you to create a <a href="http://www.bramme.net/2008/09/06/a-jquery-slider-on-cookies/">jQuery slideToggle</a> that would remember it&#8217;s last state throughout page reloads. The original idea came from Matt over at ifoh designs. He even was so kind to comment on my version. He mentioned something though I think in return is a mention from me.</p>
<p><span id="more-95"></span></p>
<p>Matt says : </p>
<blockquote><p>I am just concerned though about the link for “hide” if JS is disabled it does not function? Maybe if you created the link with jQuery as well, and added it in, then it would be more accessible.</p>
</blockquote>
<p>Now even though there&#8217;s not a lot of people out there who don&#8217;t have javascript enabled, I think they should be considered. Though Matt&#8217;s idea of adding the link through JS is good, there&#8217;s an easier way. </p>
<p>What we&#8217;ll do is simply hide the link with CSS and then show it with Javascript. If people have got javascript turned off, they&#8217;ll never see it! And something tells me people with javascript turned off, won&#8217;t go snooping through your source code and wonder why they&#8217;re not seeing that link.</p>
<p>So how do we do it?</p>
<p>
#top { display: none}
</p>
<p>In my example, I&#8217;m not only hiding the link, I&#8217;m also hiding the container. You could, offcourse, only hide the link.</p>
<p>And how does our new slider.js code look like?</p>
<p>
$(document).ready(function() {</p>
<p>	// the div that will be hidden/shown<br />
	var panel = $(&#8220;#box&#8221;);<br />
	//the button that will toggle the panel<br />
	var button = $(&#8220;#top a&#8221;);<br />
	var button_container = $(&#8220;#top&#8221;);<br />
	// do you want the panel to start off collapsed or expanded?<br />
	var initialState = &#8220;expanded&#8221;; // &#8220;expanded&#8221; OR &#8220;collapsed&#8221;<br />
	// the class added when the panel is hidden<br />
	var activeClass = &#8220;hidden&#8221;;<br />
	// the text of the button when the panel&#8217;s expanded<br />
	var visibleText = &#8220;hide&#8221;;<br />
	// the text of the button when the panel&#8217;s collapsed<br />
	var hiddenText = &#8220;show&#8221;;</p>
<p>	//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
	// don&#8217;t    edit    below    this    line,<br />
	// unless you really know what you&#8217;re doing<br />
	//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>	button_container.show();</p>
<p>	if($.cookie(&#8220;panelState&#8221;) == undefined) {<br />
		$.cookie(&#8220;panelState&#8221;, initialState);<br />
	} </p>
<p>	var state = $.cookie(&#8220;panelState&#8221;);</p>
<p>	if(state == &#8220;collapsed&#8221;) {<br />
		panel.hide();<br />
		button.text(hiddenText);<br />
		button.addClass(activeClass);<br />
	}</p>
<p>	button.click(function(){<br />
		if($.cookie(&#8220;panelState&#8221;) == &#8220;expanded&#8221;) {<br />
			$.cookie(&#8220;panelState&#8221;, &#8220;collapsed&#8221;);<br />
			button.text(hiddenText);<br />
			button.addClass(activeClass);<br />
		} else {<br />
			$.cookie(&#8220;panelState&#8221;, &#8220;expanded&#8221;);<br />
			button.text(visibleText);<br />
			button.removeClass(activeClass);<br />
		}</p>
<p>		panel.slideToggle(&#8220;slow&#8221;);</p>
<p>		return false;<br />
	});<br />
});
</p>
<p>That&#8217;s all there is to it!</p>



Bookmark:


	<a rel="nofollow" id="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F%26amp%3Btitle%3DAddition%2520to%2520jQuery%2520slider%26amp%3Bbodytext%3DA%2520few%2520days%2520ago%2520I%2520posted%2520a%2520script%2520that%2520allowed%2520you%2520to%2520create%2520a%2520jQuery%2520slideToggle%2520that%2520would%2520remember%2520it%2527s%2520last%2520state%2520throughout%2520page%2520reloads.%2520The%2520original%2520idea%2520came%2520from%2520Matt%2520over%2520at%2520ifoh%2520designs.%2520He%2520even%2520was%2520so%2520kind%2520to%2520comment%2520on%2520my%2520version.%2520He%2520ment';" title="Digg"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" id="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F%26amp%3Btitle%3DAddition%2520to%2520jQuery%2520slider%26amp%3Bnotes%3DA%2520few%2520days%2520ago%2520I%2520posted%2520a%2520script%2520that%2520allowed%2520you%2520to%2520create%2520a%2520jQuery%2520slideToggle%2520that%2520would%2520remember%2520it%2527s%2520last%2520state%2520throughout%2520page%2520reloads.%2520The%2520original%2520idea%2520came%2520from%2520Matt%2520over%2520at%2520ifoh%2520designs.%2520He%2520even%2520was%2520so%2520kind%2520to%2520comment%2520on%2520my%2520version.%2520He%2520ment';" title="del.icio.us"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" id="stumbleupon" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F%26amp%3Btitle%3DAddition%2520to%2520jQuery%2520slider';" title="StumbleUpon"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" id="technorati" target="_blank" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F';" title="Technorati"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" id="reddit" target="_blank" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F%26amp%3Btitle%3DAddition%2520to%2520jQuery%2520slider';" title="Reddit"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitthis" target="_blank" href="javascript:window.location='';" title="TwitThis"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>
	<a rel="nofollow" id="facebook" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F%26amp%3Bt%3DAddition%2520to%2520jQuery%2520slider';" title="Facebook"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" id="email" target="_blank" href="javascript:window.location='mailto%3A%3Fsubject%3DAddition%2520to%2520jQuery%2520slider%26amp%3Bbody%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Faddition-to-jquery-slider%252F';" title="E-mail this story to a friend!"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/email_link.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.bramme.net/2008/09/addition-to-jquery-slider/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A jQuery slider on cookies</title>
		<link>http://www.bramme.net/2008/09/a-jquery-slider-on-cookies/</link>
		<comments>http://www.bramme.net/2008/09/a-jquery-slider-on-cookies/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 09:22:11 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.bramme.net/?p=59</guid>
		<description><![CDATA[As you&#8217;ll probably have noticed, the homepage of my site featues a &#8220;recent project&#8221; box that can be closed with a little jQuery sliding effect. I allready planned that feature in the previous version, but found it extremely annoying becasue with every refresh, it would open again. A while back I found this article on [...]]]></description>
			<content:encoded><![CDATA[<p>As you&#8217;ll probably have noticed, the <a href="http://www.bramme.net">homepage of my site</a> featues a &#8220;recent project&#8221; box that can be closed with a little jQuery sliding effect. I allready planned that feature in the previous version, but found it extremely annoying becasue with every refresh, it would open again. A while back I found <a href="http://ifohdesigns.com/blog/tutorials/unobtrusive-jquery-slide-toggle-with-cookies">this article on ifohdesigns</a> and decided to use it to implement the sliding box again in my site, though this time, it would stay closed.</p>
<p>However, the script started off from a hidden div that you want to show, instead of hiding a box that&#8217;s there. I started fiddling with it and before I knew it, I broke it. Nice going, Bram. Smooth.<br />
<span id="more-59"></span></p>
<p>The script was completely unobtrusive, which means there&#8217;s no javascript in your actual html. That part I really liked, but the script had some major downsides too: if you knew nothing about jQuery, you had to use the accompanying HTML else it wouldn&#8217;t work either. So I decided to rewrite the whole thing and make my own script! Still, credit to ifoh designs for coming up with the idea!</p>
<p>So what do you need to get it working in the first place?</p>
<ul>
<li>offcourse <a href="http://docs.jquery.com/Downloading_jQuery">the latest jQuery version</a></li>
<li><a href="http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/">the jQuery cookie plugin by Klaus Hartl</a></li>
</ul>
<p>Let&#8217;s start off with some basic HTML. What we need is a box to hide and a link to hide it! I&#8217;m just pasting something here, but you can offcourse whip up something yourself!</p>
<p><a href="http://bramme.net/wp-content/jqueryhide_demo/html.html" target="_blank" class="demolink"><img src="/wp-content/themes/brammev2/images/demo_button.gif" alt="See the demo" /></a></p>
<p>(I&#8217;m not gonna post the html in here, just visit the links and view the source)</p>
<p>When you click the hide link now, you can see nothing happens. You&#8217;re simply taken to the same page with a # behind it. We&#8217;re gonna fix that now! Let&#8217;s add some jQuery love, like you would before you read this.</p>
<p><a href="http://bramme.net/wp-content/jqueryhide_demo/toggle.html" target="_blank" class="demolink"><img src="/wp-content/themes/brammev2/images/demo_button.gif" alt="See the demo" /></a></p>
<p>As you can see, our script now works! Yes! When you click the link, the #box div closes. Click it again, and there it is again! Merely toggling away.</p>
<p>How does it work, you might ask?</p>
<pre class="brush: jscript;">
$(document).ready(function() {
	$(&quot;#top a&quot;).click(function() {
		$(&quot;#box&quot;).slideToggle();
	});
});
</pre>
<p>As simple as that. But you knew that offcourse.</p>
<p>However, there are some downsides to the current version.</p>
<ul>
<li>Close it and then hit refresh. It&#8217;s open again.</li>
<li>Once you click it, you end up with # at the end of your url. Not the pretiest thing.</li>
<li>Close it. See how our link stays exactly the same?</li>
<li>Imagine we want to start off with our box hidden. Changing the CSS to display:none is easy now, but imagine you gotta start digging through 100 of lines of CSS.</li>
</ul>
<p>But, no worries because all those things are covered with this script! </p>
<p><a href="http://bramme.net/wp-content/jqueryhide_demo/cookiestoggle.html" target="_blank" class="demolink"><img src="/wp-content/themes/brammev2/images/demo_button.gif" alt="See the demo" /></a></p>
<p>All our previous downsides are now fixed. Our box stays closed, there&#8217;s no # at the of the url, no matter how many times you click the link. The text of the link even changes and a class is added if you want to add different styling. Even the fourth downside is covered in the script!</p>
<p>So how does it look?</p>
<pre class="brush: jscript;">
$(document).ready(function() {
        // the div that will be hidden/shown
	var panel = $(&quot;#box&quot;);
	//the button that will toggle the panel
	var button = $(&quot;#top a&quot;);
	// do you want the panel to start off collapsed or expanded?
	var initialState = &quot;expanded&quot;; // &quot;expanded&quot; OR &quot;collapsed&quot;
	// the class added when the panel is hidden
	var activeClass = &quot;hidden&quot;;
	// the text of the button when the panel's expanded
	var visibleText = &quot;hide&quot;;
	// the text of the button when the panel's collapsed
	var hiddenText = &quot;show&quot;;
        //---------------------------
	// don't    edit    below    this    line,&lt;
	// unless you really know what you're doing
	//---------------------------&lt;/p&gt;
	if($.cookie(&quot;panelState&quot;) == undefined) {
		$.cookie(&quot;panelState&quot;, initialState);
	}
	var state = $.cookie(&quot;panelState&quot;);
	if(state == &quot;collapsed&quot;) {
		panel.hide();
		button.text(hiddenText);
		button.addClass(activeClass);
	}
	button.click(function(){
		if($.cookie(&quot;panelState&quot;) == &quot;expanded&quot;) {
			$.cookie(&quot;panelState&quot;, &quot;collapsed&quot;);
			button.text(hiddenText);&lt;
			button.addClass(activeClass);
		} else {
			$.cookie(&quot;panelState&quot;, &quot;expanded&quot;);
			button.text(visibleText);
			button.removeClass(activeClass);
		}
		panel.slideToggle(&quot;slow&quot;);
		return false;
	});
});
</pre>
<p>Paste that lot in your &lt;script&gt; tags (you could offcourse put it in a separate .js file), change the configuration at the top to your needs and you&#8217;re good to go!</p>
<p>Move on to the next page if you want to know how the whole thing works.</p>



Bookmark:


	<a rel="nofollow" id="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F%26amp%3Btitle%3DA%2520jQuery%2520slider%2520on%2520cookies%26amp%3Bbodytext%3DAs%2520you%2527ll%2520probably%2520have%2520noticed%252C%2520the%2520homepage%2520of%2520my%2520site%2520featues%2520a%2520%2522recent%2520project%2522%2520box%2520that%2520can%2520be%2520closed%2520with%2520a%2520little%2520jQuery%2520sliding%2520effect.%2520I%2520allready%2520planned%2520that%2520feature%2520in%2520the%2520previous%2520version%252C%2520but%2520found%2520it%2520extremely%2520annoying%2520becasue%2520with%2520ever';" title="Digg"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" id="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F%26amp%3Btitle%3DA%2520jQuery%2520slider%2520on%2520cookies%26amp%3Bnotes%3DAs%2520you%2527ll%2520probably%2520have%2520noticed%252C%2520the%2520homepage%2520of%2520my%2520site%2520featues%2520a%2520%2522recent%2520project%2522%2520box%2520that%2520can%2520be%2520closed%2520with%2520a%2520little%2520jQuery%2520sliding%2520effect.%2520I%2520allready%2520planned%2520that%2520feature%2520in%2520the%2520previous%2520version%252C%2520but%2520found%2520it%2520extremely%2520annoying%2520becasue%2520with%2520ever';" title="del.icio.us"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" id="stumbleupon" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F%26amp%3Btitle%3DA%2520jQuery%2520slider%2520on%2520cookies';" title="StumbleUpon"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" id="technorati" target="_blank" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F';" title="Technorati"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" id="reddit" target="_blank" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F%26amp%3Btitle%3DA%2520jQuery%2520slider%2520on%2520cookies';" title="Reddit"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitthis" target="_blank" href="javascript:window.location='';" title="TwitThis"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>
	<a rel="nofollow" id="facebook" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F%26amp%3Bt%3DA%2520jQuery%2520slider%2520on%2520cookies';" title="Facebook"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" id="email" target="_blank" href="javascript:window.location='mailto%3A%3Fsubject%3DA%2520jQuery%2520slider%2520on%2520cookies%26amp%3Bbody%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F09%252Fa-jquery-slider-on-cookies%252F';" title="E-mail this story to a friend!"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/email_link.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.bramme.net/2008/09/a-jquery-slider-on-cookies/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Addition to Auth Library tutorial</title>
		<link>http://www.bramme.net/2008/07/addition-to-auth-library-tutorial/</link>
		<comments>http://www.bramme.net/2008/07/addition-to-auth-library-tutorial/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 13:04:50 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.bramme.net/?p=39</guid>
		<description><![CDATA[Okay, I&#8217;ve decided to write a little addition to my previous tutorial. I finished the last tutorial, my Auth library for CI, with a note: When you use this library, you’ll have to manually adjust the standard redirects from the library. I just set them to /admin and /admin/login but this could/will be different for [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, I&#8217;ve decided to write a little addition to my previous tutorial. I finished the last tutorial, <a href="http://www.bramme.net/2008/07/29/auth-library-for-codeigniter-tutorial/">my Auth library for CI</a>, with a note:<br />
<em>When you use this library, you’ll have to manually adjust the standard redirects from the library. I just set them to /admin and /admin/login but this could/will be different for you offcourse. A better way would be to add an initialize() method that gets a config array passed to set these URI’s in class vars.</em></p>
<p>I have adjusted my library a little so you can initialize it, setting redirects as you please. Even more, it now automatically uses any auth.php config files too. This was really easy and I thank <a href="http://codeigniter.com/forums/member/39794/">xwero</a> for helping me figure it out, over at the CodeIgniter community.<br />
<span id="more-39"></span><br />
All we need to do is add 2 more class variables, an extra method and let the construct expect an array. Doing this makes our library look like.</p>
<p><strong>Note: I deleted functions that remained the same, so don&#8217;t just overwrite everything!</strong></p>
<pre class="brush: php;">
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Auth {

	var $CI 			= NULL;
	var $index_redirect	= '/admin';
	var $login_redirect	= '/admin/login';

	function Auth($props = array())
	{
		$this-&gt;CI =&amp; get_instance();

		// Load additional libraries, helpers, etc.
		$this-&gt;CI-&gt;load-&gt;library('session');
		$this-&gt;CI-&gt;load-&gt;database();
		$this-&gt;CI-&gt;load-&gt;helper('url');

		if (count($props) &gt; 0)
		{
			$this-&gt;initialize($props);
		}
	}

	/**
	 * initialize class preferences
	 *
	 * @access	public
	 * @param	array
	 * @return	void
	 */
	function initialize($props = array())
	{
		if (count($props) &gt; 0)
		{
			foreach ($props as $key =&gt; $val)
			{
				$this-&gt;$key = $val;
			}
		}
	}

	/**
	 *
	 * This function redirects users after logging in
	 *
	 * @access	public
	 * @return	void
	 */
	function redirect()
	{
		if ($this-&gt;CI-&gt;session-&gt;userdata('redirected_from') == FALSE)
		{
			redirect($this-&gt;index_redirect);
		} else {
			redirect($this-&gt;CI-&gt;session-&gt;userdata('redirected_from'));
		}

	}

	/**
	 *
	 * This function restricts users from certain pages.
	 * use restrict(TRUE) if a user can't access a page when logged in
	 *
	 * @access	public
	 * @param	boolean	wether the page is viewable when logged in
	 * @return	void
	 */
	function restrict($logged_out = FALSE)
	{
		// If the user is logged in and he's trying to access a page
		// he's not allowed to see when logged in,
		// redirect him to the index!
		if ($logged_out &amp;&amp; $this-&gt;logged_in())
		{
			redirect($this-&gt;index_redirect);
		}

		// If the user isn' logged in and he's trying to access a page
		// he's not allowed to see when logged out,
		// redirect him to the login page!
		if ( ! $logged_out &amp;&amp; ! $this-&gt;logged_in())
		{
			$this-&gt;CI-&gt;session-&gt;set_userdata('redirected_from', $this-&gt;CI-&gt;uri-&gt;uri_string()); // We'll use this in our redirect method.
			redirect($this-&gt;login_redirect);
		}
	}
}

/* End of file: Auth.php */
/* Location: application/libraries/Auth.php */
</pre>
<p>How does this work? When you load a library, the Loader class automatically scans for a config file with the same name as your class. So when we add auth.php to our config folder, we can pass the index_redirect and login_redirect variables to our library when we load it.</p>
<p>On one of my applications, my config file looks like this:</p>
<pre class="brush: php;">
&lt;?php
	$config['index_redirect'] = '/general';
	$config['login_redirect'] = '/log/in';

/* End of file: auth.php */
/* Location: application/config/auth.php */
</pre>
<p>If you don&#8217;t like using config files, you could set that array in your method and use it when loading the Auth library, or when your autoloading the library, you could use the initialize() function.</p>



Bookmark:


	<a rel="nofollow" id="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F%26amp%3Btitle%3DAddition%2520to%2520Auth%2520Library%2520tutorial%26amp%3Bbodytext%3DOkay%252C%2520I%2527ve%2520decided%2520to%2520write%2520a%2520little%2520addition%2520to%2520my%2520previous%2520tutorial.%2520I%2520finished%2520the%2520last%2520tutorial%252C%2520my%2520Auth%2520library%2520for%2520CI%252C%2520with%2520a%2520note%253A%250D%250AWhen%2520you%2520use%2520this%2520library%252C%2520you%25E2%2580%2599ll%2520have%2520to%2520manually%2520adjust%2520the%2520standard%2520redirects%2520from%2520the%2520library.%2520I%2520just%2520set';" title="Digg"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" id="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F%26amp%3Btitle%3DAddition%2520to%2520Auth%2520Library%2520tutorial%26amp%3Bnotes%3DOkay%252C%2520I%2527ve%2520decided%2520to%2520write%2520a%2520little%2520addition%2520to%2520my%2520previous%2520tutorial.%2520I%2520finished%2520the%2520last%2520tutorial%252C%2520my%2520Auth%2520library%2520for%2520CI%252C%2520with%2520a%2520note%253A%250D%250AWhen%2520you%2520use%2520this%2520library%252C%2520you%25E2%2580%2599ll%2520have%2520to%2520manually%2520adjust%2520the%2520standard%2520redirects%2520from%2520the%2520library.%2520I%2520just%2520set';" title="del.icio.us"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" id="stumbleupon" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F%26amp%3Btitle%3DAddition%2520to%2520Auth%2520Library%2520tutorial';" title="StumbleUpon"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" id="technorati" target="_blank" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F';" title="Technorati"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" id="reddit" target="_blank" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F%26amp%3Btitle%3DAddition%2520to%2520Auth%2520Library%2520tutorial';" title="Reddit"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitthis" target="_blank" href="javascript:window.location='';" title="TwitThis"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>
	<a rel="nofollow" id="facebook" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F%26amp%3Bt%3DAddition%2520to%2520Auth%2520Library%2520tutorial';" title="Facebook"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" id="email" target="_blank" href="javascript:window.location='mailto%3A%3Fsubject%3DAddition%2520to%2520Auth%2520Library%2520tutorial%26amp%3Bbody%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Faddition-to-auth-library-tutorial%252F';" title="E-mail this story to a friend!"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/email_link.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.bramme.net/2008/07/addition-to-auth-library-tutorial/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Auth library for CodeIgniter tutorial.</title>
		<link>http://www.bramme.net/2008/07/auth-library-for-codeigniter-tutorial/</link>
		<comments>http://www.bramme.net/2008/07/auth-library-for-codeigniter-tutorial/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 15:32:18 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.bramme.net/?p=26</guid>
		<description><![CDATA[Hello and welcome to my first tutorial in ages. Today, I&#8217;ll be explaining to you how you can create a simple login (or &#8216;auth&#8217;, short for authorization) library for CodeIgniter. What functionality will our library have? Users accessing restricted pages will see a login screen if they’re not logged in, once logged in they’ll be [...]]]></description>
			<content:encoded><![CDATA[<p>Hello and welcome to my first tutorial in ages. Today, I&#8217;ll be explaining to you how you can create a simple login (or &#8216;auth&#8217;, short for authorization) library for CodeIgniter.</p>
<p>What functionality will our library have? Users accessing restricted pages will see a login screen if they’re not logged in, once logged in they’ll be redirected to the page where they were, or simply see a standard page. That’s about it for this tutorial, though adding things like user groups, restrictions etc… could all be added later. Maybe stuff for a later tutorial?</p>
<p>I&#8217;ll copy/paste bits of code from my own (simple) auth library and then explain what they do. So at the end of this tutorial you will have a working library.<br />
<span id="more-26"></span><br />
For starters, let’s quickly rethink how we used to do this kind of stuff with procedural PHP. What I used to do was set a $_SESSION variable, most of the time $_SESSION[‘loggedin’] = true; or something like that. If it was false, or not set, I would check if cookies were set with the username and/or hashed password. Or I would check if the submit form was submitted. I would process that data and see if a valid user entered his/her name and password. It was then but a case to create an index page that checked for that session variable to display the login form or the actual content. Probably not the safest way, but it worked like a charm.</p>
<p>With CodeIgniter and the MVC approach, things aren’t that different. But, they can be a little confusing at first. I know they were for me, that’s why I’m writing this tutorial.<br />
Before we start, let me just say I&#8217;m presuming some things:<br />
- You have some knowledge of PHP and you grasp some basic Object Oriented PHP lingo, so you won&#8217;t have to ask <em>&#8220;Excuse me, but what does &#8216;construct&#8217; mean?&#8221;</em>.<br />
- Comes with the previous one: you at least know what CodeIgniter is. If you don&#8217;t know what CI is, I suggest you first go read the <a href="http://codeigniter.com/user_guide/">user guide</a>, which is excellent by the way.<br />
- You use a mysql database to store your users and their passwords. If you haven&#8217;t got a user table yet, but you&#8217;re planning on using one, here&#8217;s some SQL to help you create a table. It also adds a standard user, named User with the password Applepie.</p>
<pre class="brush: sql;">
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

INSERT INTO `users` SET `username` = 'User', `password` = 'Applepie';
</pre>
<p>Okay, your still here. Your users need to login to see certain pages, so you’ll offcourse need a controller.</p>
<pre class="brush: php;">
&lt;?php
class Admin extends Controller {

	function Admin()
	{
		parent::Controller();
	}

	function index()
	{
		// Show some fancy stuff, like a dashboard
		// OR, just use index as a placeholder for another, 'standard', method, like so:
		$this-&gt;news();
	}

	function news()
	{
		// Post some news items
	}

}
?&gt;
</pre>
<p>We could add some lines code to all of our methods that check for a session variable (because that&#8217;s what we&#8217;ll use here too), and then decide to display the actual page or a login view. But we&#8217;d have to repeat it in every method and if we&#8217;d have to change it, we&#8217;d have to change it in every method. That&#8217;s not very <acronym title="Don't Repeat Yourself">DRY</acronym>, is it? </p>
<p>You know what we&#8217;ll do? Let&#8217;s create an additional method, which sole function will be displaying and processing the login form. If we&#8217;re not logged in, we can redirect to it. So let&#8217;s add our login method.</p>
<pre class="brush: php;">
&lt;?php
class Admin extends Controller {

	function Admin()
	{
		parent::Controller();

	}

	function index()
	{
		// Show some fancy stuff, like a dashboard
		// OR, just use index as a placeholder for another, 'standard', method, like so:
		$this-&gt;news();
	}

	function news()
	{
		// Post some news items
	}

	function login()
	{
		$this-&gt;load-&gt;view('login');
	}
}
?&gt;
</pre>
<p>and our view file could look like this:</p>
<pre class="brush: xml;">
&lt;h2&gt;Please log in first&lt;/h2&gt;
&lt;form action=&quot;/index.php/admin/login&quot; method=&quot;post&quot;&gt;
&lt;!-- as you can see, the action refers to the same page --&gt;
&lt;p&gt;
	&lt;label for=&quot;username&quot;&gt;Username: &lt;br /&gt;
	&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&quot; id=&quot;username&quot;  /&gt;&lt;/label&gt;
&lt;/p&gt;
&lt;p&gt;
	&lt;label for=&quot;password&quot;&gt;Password: &lt;br /&gt;
	&lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;&quot; id=&quot;password&quot;  /&gt;&lt;/label&gt;
&lt;/p&gt;
&lt;p&gt;
	&lt;input type=&quot;submit&quot; name=&quot;submLogin&quot; value=&quot;Log In&quot;  /&gt;
&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>For the sake of shortness, I won&#8217;t include validation for this form, this is easy enough to figure out yourself. </p>
<p>So far for displaying the form, on to the next bit: processing it!</p>
<p>Again, for the sake of shortness, I&#8217;m not including any cookies either. If you want to store the username and password in cookies, this is easy enough to do yourself.</p>
<p>Let&#8217;s rewrite our login method so it can process the data from the login.</p>
<pre class="brush: php;">
function login()
{
	if ($this-&gt;input-&gt;post('submLogin') != FALSE)
	{
		$login = array($this-&gt;input-&gt;post('username'), $this-&gt;input-&gt;post('password'));
		if($this-&gt;auth-&gt;process_login($login))
		{
			// Login successful, let's redirect.
			$this-&gt;auth-&gt;redirect();
		}
		else
		{
			$data['error'] = 'Login failed, please try again';
			$this-&gt;load-&gt;vars($data);
		}
	}
	$this-&gt;load-&gt;view('login');
}
</pre>
<p>Whoa, whoa. $this->auth? I skipped a step, don&#8217;t worry. If you would submit your form now, you&#8217;d get a pretty error saying it can&#8217;t find the process_login method or the auth library. That&#8217;s because &#8216;auth&#8217; is the library we&#8217;re going to create. CI can&#8217;t know what&#8217;s not there yet. We&#8217;ll do that in a second. First, let&#8217;s change a few things:<br />
Start with changing your login view so it looks like this.</p>
<pre class="brush: xml;">
&lt;h2&gt;Please log in first&lt;/h2&gt;
&lt;?php
if(isset($error))
{
	echo &quot;&lt;p&gt;$error&lt;/p&gt;&quot;;
}
?&gt;
&lt;form action=&quot;/CI/index.php/admin/login&quot; method=&quot;post&quot;&gt;
&lt;!-- as you can see, the action refers to the same page --&gt;
&lt;p&gt;
	&lt;label for=&quot;username&quot;&gt;Username: &lt;br /&gt;
	&lt;input type=&quot;text&quot; name=&quot;username&quot; value=&quot;&quot; id=&quot;username&quot;  /&gt;&lt;/label&gt;
&lt;/p&gt;
&lt;p&gt;
	&lt;label for=&quot;passw&quot;&gt;Password: &lt;br /&gt;
	&lt;input type=&quot;password&quot; name=&quot;password&quot; value=&quot;&quot; id=&quot;passw&quot;  /&gt;&lt;/label&gt;
&lt;/p&gt;
&lt;p&gt;
	&lt;input type=&quot;submit&quot; name=&quot;submLogin&quot; value=&quot;Log In&quot;  /&gt;
&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>Now your users will see the error message if they enter a wrong username or password. </p>
<p>Also,  let&#8217;s add the following line to your controller construct:</p>
<pre class="brush: php;">
$this-&gt;load-&gt;library('auth');
</pre>
<p>It&#8217;ll throw an error for now because the library isn&#8217;t created yet, but we&#8217;ll do that on the next page.</p>



Bookmark:


	<a rel="nofollow" id="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F%26amp%3Btitle%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.%26amp%3Bbodytext%3DHello%2520and%2520welcome%2520to%2520my%2520first%2520tutorial%2520in%2520ages.%2520Today%252C%2520I%2527ll%2520be%2520explaining%2520to%2520you%2520how%2520you%2520can%2520create%2520a%2520simple%2520login%2520%2528or%2520%2527auth%2527%252C%2520short%2520for%2520authorization%2529%2520library%2520for%2520CodeIgniter.%250D%250A%250D%250AWhat%2520functionality%2520will%2520our%2520library%2520have%253F%2520Users%2520accessing%2520restricted%2520p';" title="Digg"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" id="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F%26amp%3Btitle%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.%26amp%3Bnotes%3DHello%2520and%2520welcome%2520to%2520my%2520first%2520tutorial%2520in%2520ages.%2520Today%252C%2520I%2527ll%2520be%2520explaining%2520to%2520you%2520how%2520you%2520can%2520create%2520a%2520simple%2520login%2520%2528or%2520%2527auth%2527%252C%2520short%2520for%2520authorization%2529%2520library%2520for%2520CodeIgniter.%250D%250A%250D%250AWhat%2520functionality%2520will%2520our%2520library%2520have%253F%2520Users%2520accessing%2520restricted%2520p';" title="del.icio.us"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" id="stumbleupon" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F%26amp%3Btitle%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.';" title="StumbleUpon"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" id="technorati" target="_blank" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F';" title="Technorati"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" id="reddit" target="_blank" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F%26amp%3Btitle%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.';" title="Reddit"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitthis" target="_blank" href="javascript:window.location='';" title="TwitThis"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>
	<a rel="nofollow" id="facebook" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F%26amp%3Bt%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.';" title="Facebook"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" id="email" target="_blank" href="javascript:window.location='mailto%3A%3Fsubject%3DAuth%2520library%2520for%2520CodeIgniter%2520tutorial.%26amp%3Bbody%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F07%252Fauth-library-for-codeigniter-tutorial%252F';" title="E-mail this story to a friend!"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/email_link.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.bramme.net/2008/07/auth-library-for-codeigniter-tutorial/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>CSS Text shadows with a touch of JS</title>
		<link>http://www.bramme.net/2008/06/css-text-shadows-with-a-touch-of-js/</link>
		<comments>http://www.bramme.net/2008/06/css-text-shadows-with-a-touch-of-js/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 10:59:17 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.bramme.net/?p=23</guid>
		<description><![CDATA[I was looking at the new wpcandy theme today and noticed their latest post headline, which had a nice little shadow. I thought &#8220;hmm, I want to know how they did that. So I set out to check the code. After all, how hard could it be? If you wanna see where we&#8217;re heading: Okay, [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking at the new <a href="http://wpcandy.com/">wpcandy</a> theme today and noticed their latest post headline, which had a nice little shadow. I thought &#8220;hmm, I want to know how they did that. So I set out to check the code. After all, how hard could it be?<br />
<span id="more-23"></span><br />
If you wanna see where we&#8217;re heading:<br />
<a href="http://bramme.net/wp-content/textshadows.html" class="demolink"><img src="/wp-content/themes/brammev2/images/demo_button.gif" alt="See a demo" /></a></p>
<p>Okay, on to the fact at hand: when looking at wpcandy&#8217;s source code, I saw this</p>
<h2><a href="http://wpcandy.com/plugins/10-plugins-to-better-improve-the-wordpress-admin.html">10 Plugins to Improve the WordPress Admin</a><br />
<span>10 Plugins to Improve the WordPress Admin</span></h2>
<p>Now, that&#8217;s not really pretty I thought. So how could we clean this up? I know! Let&#8217;s replace the text with javascript. That way stuff will degrade nicely when JS and/or CSS is off.</p>
<p>Allright, so we want to write</p>
<h2>This will be our title</h2>
<p>But we want it to become</p>
<h2>This will be our title<span>This will be our title</span></h2>
<p>As you can see, I left out the &lt;a&gt; tags, seeing as I wouldn&#8217;t need them. So, with a little bit of javascript we could change this text.</p>
<p>Note: I&#8217;m using jQuery on my site, because it&#8217;s better than Mootools and Prototype (at least that&#8217;s what I think)</p>
<p>$(document).ready(function() {<br />
var hline = $(&#8216;h2&#8242;).text();<br />
var replace = &#8216;<em>&#8216; + hline + &#8216;</em>&#8216; +  &#8216;<span>&#8216; + hline + &#8216;</span>&#8216;;<br />
$(&#8216;h2&#8242;).html(replace);<br />
});</p>
<p>What this code basically does is:</p>
<ol>
<li>When the document is ready to be manipulated</li>
<li>Get the text from all h2 elements (you could place any element here, even css selectors)</li>
<li>Build the string to replace the text with.</li>
<li>Replace the text of each h2 element</li>
</ol>
<p>This is a quick &#8216;n dirty solution, probably there&#8217;s better ways of doing this, I just don&#8217;t care about it. It works and that&#8217;s what counts. As you also probably noticed, I added &lt;em&gt; tags to the first line, as I realised during coding I&#8217;d need that extra tag.</p>
<p>So now we got our correct html setup. On to the CSS!</p>
<p>h2 {<br />
font-size: 1.6em;<br />
margin: 20px 0;<br />
font-family: &#8220;Trebuchet MS&#8221;, &#8220;Lucida Sans&#8221;, Arial, sans-serif;<br />
color: #aec364; // light color<br />
position: relative;<br />
}<br />
h2 em {<br />
font-style: normal;<br />
position: absolute;<br />
display: block;<br />
z-index: 2;<br />
}<br />
h2 span {<br />
position: absolute;<br />
display: block;<br />
left: 1px;<br />
top: 1px;<br />
z-index: 1;<br />
color: #7fa200; //darker color<br />
}</p>
<p>So, this CSS is pretty straightforward I&#8217;d think. Style your h2 in the h2 selector, <strong>NOT</strong> the em tag, as your headline wouldn&#8217;t be styled if JS was turned off! I just gave it some size, margin and a light green color. We also need to set it&#8217;s position to relative, as we&#8217;ll use position:absolute on the em- and span-elements.<br />
Next piece of CSS, the em-element: we don&#8217;t need it in italics, so we set the font-style to normal and we set the position to absolute and display to block.<br />
Last, we style the span-element: same goes for our em-element: position: absolute, display: block, but we move it one pixel down and to the left, creating the shadow effect. If you&#8217;d have a look now at your headline, you&#8217;d see it works, except for the shadow showing on top of the lighter text. To fix this we set a z-index for our em and span element. You can pick any number you&#8217;d like, just make sure to set the z-index of your em higher than that of your span.</p>
<p>That should do the trick!</p>
<p>Have a look at <a href="http://bramme.net/wp-content/textshadows.html">the demo</a>!</p>
<p>I&#8217;ve tested this in the following Windows browsers and found it working:</p>
<ul>
<li>Firefox 3.0</li>
<li>Opera 9.5</li>
<li>Safari 3.1.1</li>
<li>Internet Explorer 7.0</li>
</ul>
<p>Haven&#8217;t tested it on IE6, <del datetime="2008-08-23T10:28:24+00:00">I&#8217;m guessing it won&#8217;t work, so I should probably add some conditional to the JS: if IE6, don&#8217;t execute.</del> Someone reported to me in the comments that it does work. Huzzah!</p>
<p>Downside of this method: when you select text and copy paste it, your headline gets copied twice. Really not sure if that one&#8217;s possible to fix. Or that important.</p>



Bookmark:


	<a rel="nofollow" id="digg" target="_blank" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F%26amp%3Btitle%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS%26amp%3Bbodytext%3DI%2520was%2520looking%2520at%2520the%2520new%2520wpcandy%2520theme%2520today%2520and%2520noticed%2520their%2520latest%2520post%2520headline%252C%2520which%2520had%2520a%2520nice%2520little%2520shadow.%2520I%2520thought%2520%2522hmm%252C%2520I%2520want%2520to%2520know%2520how%2520they%2520did%2520that.%2520So%2520I%2520set%2520out%2520to%2520check%2520the%2520code.%2520After%2520all%252C%2520how%2520hard%2520could%2520it%2520be%253F%250D%250A%250D%250AIf%2520you%2520wanna%2520se';" title="Digg"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow" id="del.icio.us" target="_blank" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F%26amp%3Btitle%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS%26amp%3Bnotes%3DI%2520was%2520looking%2520at%2520the%2520new%2520wpcandy%2520theme%2520today%2520and%2520noticed%2520their%2520latest%2520post%2520headline%252C%2520which%2520had%2520a%2520nice%2520little%2520shadow.%2520I%2520thought%2520%2522hmm%252C%2520I%2520want%2520to%2520know%2520how%2520they%2520did%2520that.%2520So%2520I%2520set%2520out%2520to%2520check%2520the%2520code.%2520After%2520all%252C%2520how%2520hard%2520could%2520it%2520be%253F%250D%250A%250D%250AIf%2520you%2520wanna%2520se';" title="del.icio.us"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow" id="stumbleupon" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F%26amp%3Btitle%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS';" title="StumbleUpon"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow" id="technorati" target="_blank" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F';" title="Technorati"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow" id="reddit" target="_blank" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F%26amp%3Btitle%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS';" title="Reddit"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow" id="twitthis" target="_blank" href="javascript:window.location='';" title="TwitThis"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a>
	<a rel="nofollow" id="facebook" target="_blank" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F%26amp%3Bt%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS';" title="Facebook"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow" id="email" target="_blank" href="javascript:window.location='mailto%3A%3Fsubject%3DCSS%2520Text%2520shadows%2520with%2520a%2520touch%2520of%2520JS%26amp%3Bbody%3Dhttp%253A%252F%252Fwww.bramme.net%252F2008%252F06%252Fcss-text-shadows-with-a-touch-of-js%252F';" title="E-mail this story to a friend!"><img src="http://www.bramme.net/wp-content/plugins/sociable/images/email_link.png" title="E-mail this story to a friend!" alt="E-mail this story to a friend!" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.bramme.net/2008/06/css-text-shadows-with-a-touch-of-js/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
