<?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>WallOfScribbles &#187; JQuery</title>
	<atom:link href="http://wallofscribbles.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://wallofscribbles.com</link>
	<description>The ramblings of a man</description>
	<lastBuildDate>Sat, 07 Jan 2012 01:14:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>My jQuery plugin template is open season!</title>
		<link>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/</link>
		<comments>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 15:00:55 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://wallofscribbles.com/?p=1052</guid>
		<description><![CDATA[So I&#8217;ve been doing a lot of javascript development at work recently. I&#8217;ve basically created a lot of our reusable javascript &#8216;plugins&#8217; to cover the common requirements of the work that we do. In many cases, this resides mostly (but not entirely) in online annual-report creation. Anyone that&#8217;s in the business will know that there [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been doing a lot of javascript development at work recently. I&#8217;ve basically created a lot of our reusable javascript &#8216;plugins&#8217; to cover the common requirements of the work that we do. In many cases, this resides mostly (but not entirely) in online annual-report creation.</p>
<p>Anyone that&#8217;s in the business will know that there are some common functional elements that you end up having to build with javascript: accordions, tabs, faders, sliders, lightboxes, filters, etc. Many of these items can bleed over into non-report sites, such as intranets or even public-facing sites.</p>
<p><span id="more-1052"></span></p>
<p>Now some of those are fairly straight forward: an accordion is just a simple animate of a content section, repeat per title; tabs? Same deal. So you might be pretty inclined to just write the script adhoc on a project to project basis, copy and pasting it as you go. We were doing that at my work when I started on, but I found that we kept on having to add something, remove something, add hooks at certain points etc. In some cases we used someone else&#8217;s plugin to do these things, but we ran into some issues when it came to having to tweak the plugin for our needs.</p>
<p>As I was hired on as the senior front-ender, I was given the task of trying to help simplify things in our working process. This required me to dive right into plugin development, because one spot we were spending a lot of time was reinventing the wheel project to project. Even more time was spent debugging random plugins, or lashing them together.</p>
<p>As my place of employment works with jQuery, I wanted to develop something that could be maintained in-house, and would be a good starting point for repeating functionality. I ended up developing a jQuery plugin template, which I am sharing today.</p>
<p>Now, while I can&#8217;t feature the plugins I&#8217;ve made for my work here on my site (I&#8217;m pretty sure my they&#8217;d be less than impressed with that), I feel it&#8217;s fairly safe to distribute this template (boiler plate?) to the internets. It&#8217;s a compilation of a couple tutorials, as well as my own exploration into javascript. I&#8217;d be more than happy to give credit where it&#8217;s due, but I cannot remember where the heck I got some of the snippets I integrated. If anyone reading this happens to know, leave a comment and I&#8217;ll be more than happy to give said credit.</p>
<p>Below you will find my jQuery plugin template code in it&#8217;s entirety. I&#8217;m open to suggestions on how to improve it, so feel free to comment.</p>
<pre class="brush: jscript; title: ;">
/*
*
    Name:      my.blank.plugin.js
    Purpose:   Shell to use for jQuery plugins
*/
(function ($) {
    // DONT FORGET TO NAME YOUR PLUGIN
    jQuery.fn.myPlugin = function (options, i) {
        // This handles multiple elements (like a class selector)
        if (this.length &gt; 1) {
            var a = new Array();
            this.each(
                function (i) {
                    a.push($(this).myPlugin(options, i));
                });
            return a;
        }
        var opts = $.extend({}, $().myPlugin.defaults, options);

        /* PUBLIC FUNCTIONS */

        /* reInit is a flag that you can pass in case you don't
           want to remove everything during the destroy phase. */
        this.Destroy = function (reInit) {
            var container = this;
            var reInit = (reInit != undefined) ? reInit : false;
            $(container).removeData(&quot;myPlugin&quot;);
            // this removes the flag so we can re-initialize
        };

        this.Update = function (options) {
            opts = null;
            opts = $.extend({}, $().myPlugin.defaults, options);
            this.Destroy(true);
            return this.Create();
        };

        /* iteration will give you the index of the item
           in the selection. If not part of a loop, I'm
           prett sure this will be null. You've been warned. */
        this.Create = function (iteration) {

            // this stops double initialization
            if ($(container).data(&quot;myPlugin&quot;) == true)
                return this;

            // call a function before you do anything
            if (opts.beforeCreateFunction != null &amp;&amp; $.isFunction(opts.beforeCreateFunction))
                opts.beforeCreateFunction(targetSection, opts);

             // reference to the object you're manipulating. To jQuery it, use $(container).
            var container = this;
            /* Failing that, you could just use 'this' without the var declaration,
               but if you are doing a lot of child looping, you'll be glad to have
               a reference to the target object. Also, performance improvements! */

            ////////////////////
            // DO STUFF HERE
            ////////////////////

            // Set a flag to show that this element has been plugin'd
            $(container).data(&quot;myPlugin&quot;, true);

            // call a function after you've initialized your plugin
            if (opts.afterCreateFunction != null &amp;&amp; $.isFunction(opts.afterCreateFunction))
                opts.afterCreateFunction(targetSection, opts);

            /* Make sure to return, otherwise you can't store the element in a varaible,
               which means you can't use any of the plubic functions (you may not need
               them, but it's still good practice.) */
            return this;
        };

        /* Example of a public function, this can be used if you store
           this object in a variable.

           e.g. var foo = $(&quot;#target&quot;).myPlugin();
                foo.PublicFunction();
        */

        this.PublicFunction = function () {
            // do something
            myPrivatefunction();
        };

        /* PRIVATE FUNCTIONS */

        /* These aren't accessibly externally, and so can only
           be called from within the enclosure code. */

        function myPrivatefunction() { };

        /* arguably, you could wrap your private (or public)
        functions in an array such as the following: */

        // Private helper functions
        helper = {
            firstHelper: function () { /* do something */ },
            secondHelper: function (somevalue) { /* do something with the variable passed in */ }
        };

        //Public helper functions
        this.Helper = {
            FirstHelper: function () { /* do something */ },
            SecondHelper: function (somevalue) { /* do something with the variable passed in */ }
        };

        /* Personally, I don't see the point of doing this for
           anything OTHER than helper functions, I could, and
           probably am, wrong here... or at least ignorant. */

        // Finally
        return this.Create(i);
    };

    // DONT FORGET TO NAME YOUR DEFAULTS WITH THE SAME NAME
    jQuery.fn.myPlugin.defaults = {
        foo: &quot;bar&quot;,
        something: &quot;else&quot;,

        // Remember: these are function _references_ not function calls
        beforeCreateFunction: null,
        afterCreateFunction: null
    };
})(jQuery);
</pre>
<p>I&#8217;ve tried to comment the hell out of the code, so that it&#8217;s as straight-forward as possible. I assume that anyone that uses this will strip out the comments, as well as any of the functionality that isn&#8217;t required.</p>
<p>Please feel free to use this, but If you&#8217;d be so kind as to give me credit, that&#8217;d be swell. I can&#8217;t force you, but we&#8217;re all friends out here, aren&#8217;t we?</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2011/my-jquery-plugin-is-open-season/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facelift!</title>
		<link>http://wallofscribbles.com/2011/facelift/</link>
		<comments>http://wallofscribbles.com/2011/facelift/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 15:00:12 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Fireworks]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[layouts]]></category>
		<category><![CDATA[newsite]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://wallofscribbles.com/?p=1009</guid>
		<description><![CDATA[So while I doubt many people come to my actual website anymore, anyone who has done so in the last week or so will have noticed a&#8230; minor change to the site. That is to say that I&#8217;ve utterly changed everything. Basically I was getting bored of my old one, and to be honest I [...]]]></description>
			<content:encoded><![CDATA[<p>So while I doubt many people come to my actual website anymore, anyone who has done so in the last week or so will have noticed a&#8230; minor change to the site. That is to say that I&#8217;ve utterly changed everything. Basically I was getting bored of my old one, and to be honest I wasn&#8217;t happy with any part of the design of the old layout from the get go.</p>
<p>I made the last one basically because I have an impulse to change my layout roughly once every year (or two). I was suckered into the whole pop-out style that was going around at the time, and I wanted my piece of the action. As a side-effect, The site sort of fell on its ass when certain browsers hit it. I had already given up on the layout before I had even really finished it.</p>
<p>That&#8217;s always a good sign.</p>
<p><span id="more-1009"></span>First off, you may have noticed that the site has a bit more&#8230; <em>character</em> to it. I think this layout is a bit more me. At the very least, it gives viewers a brief glimpse into the borderline insanity that is my mind. I&#8217;ve removed my resume from the site, since no one actually cares about that. I&#8217;ve split the site &#8211; quite literally &#8211; down the middle. To the left are my ramblings for your continued enjoyment. To the right is my portfolio&#8230; such as it is.</p>
<h2>Changes to the blog</h2>
<p>So on top of having a bitching yellow background, I&#8217;ve narrowed things down. Comments are on the left (a couple styles still need to be applied there) and the main section is more narrow. This makes the page longer, but is more friendly for mobile readers, and actually is fairly readable for screen-readers. Pictures should take up the whole width of the column now. I haven&#8217;t gone back into old posts to correct them, so the wide picture thing is a &#8216;from here on out&#8217; scenario.</p>
<p>Archive pages exist again for things like Categories, Tabs and such. You could probably url-hack the site to get the archives for dates &#8216;n the like, but I have opted to not include any straight-up way of getting to them. Really they&#8217;re just not that important.</p>
<p>I&#8217;ve also got search going again! I&#8217;m a but surprised that not a single person complained about that on my old design, despite it being a total oversite on my part. And when I did notice it, I had absolutely no intention of adding it (read: giving up on the layout).</p>
<h2>Changes to the portfolio</h2>
<p>The portfolio is far from complete. After talking to some designers that are far superior to myself, I&#8217;ve had some gentle instruction to rethink what I was going for. Very valid arguments that I agree with. So as of right now, it looks the way it looks. When I get done with it, it&#8217;ll look totally different. I think I may just skip to the next part, which will talk about what I did here in greater detail.</p>
<p>Suffice it to say that it&#8217;s currently an alpha layout at best. I wasn&#8217;t happy with it when I started, so think of the current layout as more of a tech-demo.</p>
<h2>Code changey stuff</h2>
<p>First off, with my running this blog in WP 3-point-whatever version it is this week, I wanted to try and take advantage of some of the new features.</p>
<p><strong>Custom content types! </strong>Gone are the hackey days of hidden categories and private posts. Now I&#8217;ve got Posts, Portfolio items, and Code items (subject to change). This allows for an easy separation of content, which has made my life so much easier. It requires me to do some custom loop stuff on the portfolio page, but man oh man is it easier.</p>
<p><strong>JQuery stuff! </strong>In the last eight months or so, I&#8217;ve gone from knowing a smattering of jQuery stuff (how to make plugins go&#8230; barely) to writing my own. This is down mostly to my being a senior front-ender at <a title="Radley Yeldar" href="http://ry.com">Radley Yeldar</a>, and the ever-pressing advances the designers want to go for with their designs.</p>
<p>Hm. That sounds bitter. I&#8217;m not, actually. It&#8217;s been a great learning experience for me, and I&#8217;m happy to have the challenge. Seriously every day it&#8217;s something new. Regardless, I&#8217;ve opted to gussy up my site with a bunch of interactions based on some more basic jQuery. Some are more obvious than others. The most noticeable one is the portfolio/code area functionality.</p>
<p>Basically I&#8217;ve ajax-ed the whole thing so that when you enter an article, it&#8217;ll load it up right there. The nice thing about it is that the fallback for non-js is that it will just link to an article-style page. Simple but effective. The final version of the portfolio will probably maintain that functionality, though the layout will be changing. I&#8217;m not sure to what yet (I was just glad to get the layout complete enough to release), but you can assume that the final version will be stunning.</p>
<h2>Other changes that are afoot</h2>
<p>I&#8217;m trying to get into a more regular posting schedule, if only to train myself to stick with anything for longer than a couple of weeks. Normally on Thursdays, but recent things (like releasing and altering a new layout) sort of took over. I&#8217;ll try and do a double-post this week. Key word: <em>try</em>.</p>
<p>I&#8217;m also going to be investing some more time into varying my posts up a bit more. I&#8217;ll be doing more tech-posting, since I&#8217;m back in that world. They will probably range between &#8216;pointless rants&#8217; to &#8216;something useful and/or downloadable.&#8217; Hopefully more of the latter, but knowing me &#8211; which I should &#8211; it&#8217;ll probably be more of the former. I&#8217;m a bit of a bitch that way.</p>
<p>Anyways, I&#8217;d love some feedback if anyone has any. I generally take constructive criticism into consideration, but saying &#8216; It sucks lol&#8217; will just be met with a sage nod, knowing that the Internet is maintaining the status quo.</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2011/facelift/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Colour Swapper in WordPress</title>
		<link>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/</link>
		<comments>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 13:05:13 +0000</pubDate>
		<dc:creator>Corey Dutson</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[colour swapper]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.wallofscribbles.com/?p=267</guid>
		<description><![CDATA[Who likes website colour swappers? Anyone? When I developed the theme I am currently using, I searched high and low for something I could steal take inspiration from. The funny thing is that most people don&#8217;t even know where the colour swapper is on my site. So few in fact that I had to add [...]]]></description>
			<content:encoded><![CDATA[<p>Who likes website colour swappers? Anyone?</p>
<p>When I developed the theme I am currently using, I searched high and low for something I could <span style="text-decoration: line-through;">steal</span> take inspiration from. The funny thing is that most people don&#8217;t even know where the colour swapper is on my site. So few in fact that I had to add a caption to it to help make it a little more obvious.</p>
<p>In any case I felt that I would share the method I used to create the swapping functionality on my website. I&#8217;m aware that most of it can be figured out with a little imagination and some source viewing, but I think that going through it step by step is more helpful.</p>
<p><strong>UPDATE &#8211; 10/09/2008 -</strong> In record time, a second pair of eyes looking at this has managed to refine the process. Thank you <a title="Bartek Gniado" href="http://bart.whahay.net">Bart</a> for pointing out that if the CSS files are named the same as the Title attribute of the Anchor, then you can drop the whole If statement. I&#8217;ve updated the tutorial accordingly</p>
<p><span id="more-267"></span></p>
<h2>Table of contents</h2>
<ul>
<li><a href="#article_idea">The idea</a></li>
<li><a href="#article_before">Before we begin</a></li>
<li><a href="#article_setup">Setting up our files</a></li>
<li><a href="#article_css">Setting up the CSS files</a></li>
<li><a href="#article_php">A dash of PHP</a></li>
<li><a href="#article_markup">The basic markup</a></li>
<li><a href="#article_java">The JavaScript</a>
<ul>
<li><a href="#article_java_import">Importing the required files</a></li>
<li><a href="#article_java_func">Adding functionality</a></li>
</ul>
</li>
<li><a href="#article_together">Pulling it all together</a>
<ul>
<li><a href="#article_together_flow">Flow</a></li>
</ul>
</li>
<li><a href="#article_conclusion">Conclusion</a></li>
</ul>
<h2 id="article_idea">The idea</h2>
<p>So you want to build a colour swapper? Easy as pie. You want to do it without causing a postback and sustains itself throughout the navigation? That&#8217;s a little more tricky, and what I wanted to do myself.</p>
<p>What, you&#8217;re too lazy to figure this out for yourself? No problem. Read on and be merry.</p>
<h2 id="article_before">Before we begin</h2>
<p>A few words before we begin:</p>
<ul>
<li>I am not a master of JavaScript, let alone <a title="JQuery" href="http://jquery.com/">JQuery</a>. I&#8217;m not a novice, but I am in no way a master of the language. There are probably many ways in which to improve my methods here. I&#8217;m going to take another crack at them and see what I can come up with. I&#8217;ll update the code to this post If I come up with anything.</li>
<li>In addition to the last note, I&#8217;m not going to explain how JQuery works. they do a <a title="JQuery: How it works" href="http://docs.jquery.com/How_jQuery_Works">fine job on their own</a>. If you don&#8217;t understand it, I suggest you take some time and become familiar with it.</li>
<li>This whole solution rides on the idea that your users allow JavaScript and Cookies. If the Cookies are disabled, the styles will still change, but will not persist between page refreshes/navigation. If JavaScript is disabled, nothing will work because I have not yet implemented a postback-friendly alternate. If I find myself with time I&#8217;ll do this as well and update this post.</li>
<li>This whole tutorial is based on the idea that you are applying this to a WordPress theme. If you&#8217;re using something else, you&#8217;ll need to alter this as you need.</li>
<li>I have noticed that in some browsers, the swapping is not instant, and you may notice somewhat of a flicker. In my case the colours go away, and then the new ones are applied. Some browsers do not do this. I have no idea why this happens.</li>
</ul>
<h2 id="article_setup">Setting up our files</h2>
<p>For this solution we&#8217;re going to need to <a title="JQuery: Featured Downloads" href="http://code.google.com/p/jqueryjs/downloads/list?q=label:Featured">get some files</a> from the JQuery website (the latest JQuery build), and create some files in our Theme folder.</p>
<p>You are going to need to make a JavaScript folder &#8211; I called mine &#8220;js&#8221; &#8211; and multiple CSS files. One will be a core style sheet that contains all of the universal CSS styles, and then alternate sheets that contain the CSS that is swapped around.</p>
<h3 id="article_setup_example">Example setup:</h3>
<ul>
<li>THEME FOLDER
<ul>
<li>style.css</li>
<li>Yellow Version.css</li>
<li>Red Version.css</li>
<li>Blue Version.css</li>
<li>Green Version.css</li>
<li>JS FOLDER
<ul>
<li>jquery.js</li>
<li>functions.js</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>The rest of this example will assume that you have that setup.</p>
<p></p>
<h2 id="article_css">Setting up the CSS files</h2>
<p>First off, you&#8217;re going to need some CSS files to play around with. Generally you would have a core css file that would contain all the styles that wouldn&#8217;t be affected by your swapper. Depending on how you use your swapper, this may or may not be the case. Some people want to totally alter the website with their swapper; I just wanted a colour change.</p>
<p><em><strong>Note:</strong> I&#8217;m not going to paste all the CSS I used to format the actual tiles, because my layout requirements will be different than yours. Set the CSS code up however you need to make your selector HTML &#8211; we&#8217;ll see that in a bit &#8211; look right.</em></p>
<p>So you have your core css file (style.css for WordPress users). Do what you need to in that file and then put it away. We won&#8217;t be touching it very much. The only addition I put in my CSS files are the following:</p>
<pre>.red {background-color:#FF3333;}
.yellow {background-color:#FBDC00;}
.green {background-color:#1FF900;}
.blue {background-color:#00CCFF;}</pre>
<p>These four styles are in my core CSS file so that my swappers always have access to correct classes for them. What this means is that regardless of what CSS file is imported, the swapper tiles will always look the same.</p>
<p>Next up we have our swapping CSS files: Blue Version, Green Version, Red Version and Yellow Version.</p>
<p><em><strong>Note:</strong> I use Yellow Version as my default, but that doesn&#8217;t come in for a little bit yet.</em></p>
<p>So in each one of these CSS files, I have the same selectors:</p>
<pre>.specialBGColor {background-color:#FBDC00;}
a, .specialColourText, #left_bar .widget table th {color:#FBDC00;}
.specialTopBorder{border-top:20px solid #FBDC00;}</pre>
<p>All of your swappable CSS files will have the same selectors with whatever colour differences you need. In my case The hex colours are going to change to whatever is appropriate for the file (yellow is #FBDC00, red is #FF3333, etc.) &#8220;specialBGColor&#8221; is basically a class I apply to anything that is going to have a swappable background. I also have a &#8220;speciallTopBorder&#8221; that does something similar, and some selectors for links and table headers to change their colours.</p>
<p>All of these swappable CSS files should be in the same directory. Since I am using WordPress my CSS files are all kept together in the same directory: the theme directory. I dislike the lack of separation but I&#8217;ll manage.</p>
<p>Once you&#8217;ve got all those set up we can move on to setting up the PHP functions</p>
<h2 id="article_php">A dash of PHP</h2>
<p>We need to add a little function to our functions.php file. The function will attempt to grab a value from the users cookie cache and return it. Pretty simple overall. In my case, if the cookie is not found, it returns a default string that is the URL to my yellow style sheet. As previously stated, the Yellow CSS file is the same.</p>
<p>Add the following to your functions.php file:</p>
<pre>function getStyleCookie()
{
   if($_COOKIE["styleHref"] != null &amp;&amp; $_COOKIE["styleHref"] != "")
   {
      echo $_COOKIE["styleHref"];
   }
   else
   {
      $url = bloginfo('template_url') + "/Yellow Version.css";
      echo $url;
   }
}</pre>
<p>What this does is if it can get the cookies value &#8211; in this case &#8220;styleHref&#8221; cookie &#8211; this is explained in the JS &#8211; it will echo the value of the cookie. if it can&#8217;t it returns the default value of the Yellow Version.css location.</p>
<p><em><strong>Note:</strong> I could have made the function a bit more universal by passing in the cookie name, but as I&#8217;m only using it for this one instance, I didn&#8217;t bother.</em></p>
<h2 id="article_markup">The basic markup</h2>
<p>Alright lets set up the markup. Lets start with the header.</p>
<p>Locate the &lt;head&gt;&lt;/head&gt; section of your website (for WordPress users, this is usually the header.php file).</p>
<p>You&#8217;ll need to find where you have any current CSS files linked and paste the following over or after the CSS tags. I say over or after because depending how much of this you&#8217;re following, you may already have a core CSS file that you don&#8217;t want to remove.</p>
<pre>&lt;!-- THIS IS YOUR CORE CSS FILE. IGNORE THIS LINE IF YOU ALREADY HAVE THIS IN YOUR HEADER --&gt;
&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css"/&gt;

&lt;!-- some dynamic stuff that won't make any sense yet, but it will later --&gt;
&lt;?php if($_COOKIE["styleHref"] != null &amp;&amp; $_COOKIE["styleHref"] != "") : ?&gt;
   &lt;link id="altsheet" rel="stylesheet" href="&lt;?php echo $_COOKIE["styleHref"]; ?&gt;" type="text/css"/&gt;
&lt;?php else : ?&gt;
   &lt;link id="altsheet" rel="stylesheet" href="&lt;?php bloginfo('template_url'); ?&gt;/Yellow Version.css" type="text/css"/&gt;
&lt;?php endif; ?&gt;</pre>
<p><strong>What this does:</strong> try and get the URL to the currently used CSS file and render that CSS file. If nothing can be found, default to the Yellow Version style sheet. This may seem repetitive given that the functions.php code we put in also compensates for a lack of cookie. I&#8217;m one for having backup plans, and this was mine. If you don&#8217;t like it, you can simply remove the entire else section.</p>
<p></p>
<p>Next up is the actual swapper markup. I&#8217;m going to paste a simplified version of mine, because as I stated before, your swapper will look different than mine. As long as the base HMTL is the same, this solution works. how you style things up is totally up to you.</p>
<pre>&lt;div id="colour_swapper"&gt;
   &lt;ul&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="green" title="Green Version"&gt;Swap to green&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="yellow" title="Yellow Version"&gt;Swap to yellow&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="blue" title="Blue Version"&gt;Swap to blue&lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
         &lt;a href="javascript:;" class="red" title="Red Version"&gt;Swap to red&lt;/a&gt;
      &lt;/li&gt;
   &lt;/ul&gt;
&lt;/div&gt;</pre>
<p>The important parts to this are two-fold.</p>
<p>First, the <strong>ID of the DIV</strong> element is going to be used in the JavaScript. I&#8217;m aware that the DIV isn&#8217;t really needed, but as I said, this is a simplified version of what I&#8217;m using, and as such I&#8217;ve actually stripped out additional elements contained in the DIV.</p>
<p>The second thing is the <strong>title of the ANCHOR items</strong>. The title will be used in the JavaScript to determine what CSS file to get.</p>
<p><em><strong>Note:</strong> I&#8217;m aware that having a href of &#8216;javascript:;&#8217; is a bad practice. In reality the href should offer a link that would manually post back the page, and have PHP  code that would look for that action and set the cookie that way. Doing so would allow for the swapper to work when JavaScript is disabled. </em></p>
<h2 id="article_java">The JavaScript</h2>
<h3 id="article_java_import">Importing the required files</h3>
<p>This is where everything is tied together and starts working.</p>
<p>First off we need to import some JS files. This solution imports JQuery, as well as a functions.js file that I created in a sub directory of my Theme folder called &#8220;js&#8221;. This was explained in the <a title="Setting up your files" href="#article_setup">Setup</a> section, but I&#8217;m still reiterating.</p>
<p>First we need to add a bit more markup within our &lt;head&gt;&lt;/head&gt; sections:</p>
<pre>&lt;script type="text/JavaScript" src="&lt;?php bloginfo('template_url'); ?&gt;/js/jquery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
   var templateUrl = "&lt;?php bloginfo('template_url'); ?&gt;";
&lt;/script&gt;
&lt;script type="text/JavaScript" src="&lt;?php bloginfo('template_url'); ?&gt;/js/functions.js"&gt;&lt;/script&gt;</pre>
<p>What we&#8217;re doing here:</p>
<ol>
<li>Importing the JQuery framework.</li>
<li>Setting a JavaScript variable that contains the template_url value from WordPress. This makes life easier when we get to the actual cookie setting.</li>
<li>Importing our functions.js file that will contain all of our functionality.</li>
</ol>
<p><em><strong>Note:</strong> I&#8217;m not a JavaScript expert, and part of me thinks that there should be a way to pass in the template URL to the functions.js import call. I don&#8217;t know and if someone can comment on this and explain, I&#8217;d be more than happy to refine my work.</em></p>
<p>Next up we need to add some functionality to the functions.js file!</p>
<h3 id="article_java_func">Adding functionality</h3>
<p>I&#8217;ll pase the code first and then explain it:</p>
<pre>var $j = jQuery.noConflict();

$j(document).ready(function(){
   $j("#colour_swapper a").click( function(){
	var style_title = $j(this).attr("title");
	var style_href= "";
	var altSheet = $j("#altsheet");

	altSheet.attr({"href":templateUrl +"/" + style_title+".css"});

	style_href = altSheet.attr("href");

	var date = new Date();
	date.setTime(date.getTime()+(1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "styleHref="+style_href+"; expires="+date.toGMTString()+"; path=/";
   });
 });</pre>
<p><strong>What the hell is going on:</strong></p>
<ol>
<li>Set JQuery to no conflict mode by assigning it to a different variable. I had to do this because of other plugins I use on my site. I maintain that this is a good idea when you are working in a plugin-heavy framework like WordPress.</li>
<li>For every link found within the element with the id of colour_swapper attach a new click function</li>
<li>Set a variable to the value of the links title (Yellow Version, Green Version, etc)</li>
<li>Grab the element with the id of &#8220;altsheet&#8221; (in our case this is a link element in our header) and change it&#8217;s href value to the url of the new style sheet. This url is made up of the template URL and the name of the style sheet (title of the anchor)</li>
<li>Set a variable with the URL the new CSS file to be used.</li>
<li>Create a date variable, and set it to the future (exact day eludes me at the moment. Part of me thinks that it&#8217;s either 24 hours into the future, or 1000 days. I can&#8217;t honestly remember at this point).</li>
<li>Convert the date to a GMTString which is used in the creation of cookies.</li>
<li>Create a cookie, &#8220;styleHref&#8221;, that stores the URL of the style sheet used. This cookie is set to expire in the future, and will apply to all pages within the website (that&#8217;s what the &#8220;/&#8221; is for)</li>
</ol>
<p>Whew! That took longer to explain than I thought.</p>
<h2 id="article_together">Pulling it all together</h2>
<p>So now that we&#8217;ve gone through all that, everything should work. To give you an idea on how it all comes together I&#8217;ll explain the general flow.</p>
<h3 id="article_together_flow">Flow</h3>
<ul>
<li>While the page is loading, try and grab the cookie and set the secondary style sheet. If none is found, apply a default (Yellow Version)</li>
<li>Import all of the JavaScript needed.</li>
<li>User clicks on a style swapping link</li>
<li>JavaScript figures out what style sheet to use</li>
<li>Changes the secondary sheets href to the new CSS file</li>
<li>Sets a cookie to store the URL of the new CSS file</li>
</ul>
<p>Now if a user navigates to another page in your website, the first step will apply the change right off the bat.</p>
<h2 id="article_conclusion">Conclusion</h2>
<p>I hope that this helps someone out there, and comments are more than appreciated to help improve the solution. I&#8217;ll try and refine the solution a little more when I have time.</p>
<p>Good luck and happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://wallofscribbles.com/2008/colour-swapper-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

