<?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>Steve Taylor &#187; jQuery</title>
	<atom:link href="http://sltaylor.co.uk/blog/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://sltaylor.co.uk</link>
	<description>Freelance WordPress developer in London - XHTML, CSS &#38; design</description>
	<lastBuildDate>Mon, 19 Jul 2010 09:39:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Adding scripts to the WordPress login or registration form</title>
		<link>http://sltaylor.co.uk/blog/adding-scripts-wordpress-login-registration-form/</link>
		<comments>http://sltaylor.co.uk/blog/adding-scripts-wordpress-login-registration-form/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 22:24:49 +0000</pubDate>
		<dc:creator>Steve Taylor</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://sltaylor.co.uk/?p=295</guid>
		<description><![CDATA[Most WP developers know by now that the right way to add script libraries from a plugin or theme is to use the wp_enqueue_script function. It makes sure scripts aren&#8217;t loaded more than once, and so on. However, I just had some problems getting some jQuery to run on the login / registration form. I [...]]]></description>
			<content:encoded><![CDATA[<p>Most WP developers know by now that the right way to add script libraries from a plugin or theme is to use the <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script"><code>wp_enqueue_script</code></a> function. It makes sure scripts aren&#8217;t loaded more than once, and so on.</p>
<p>However, I just had some problems getting some jQuery to run on the login / registration form. I used <code>wp_enqueue_script</code> like a good WP developer but&#8230; no go. Where&#8217;s my jQuery?</p>
<p>It seems the answer lies in the fact that after scripts are enqueued in a normal WP page, they get spit out by something inside the call to <code>wp_head()</code>. I guess you could add <code>wp_head()</code> to the login header, but who wants all those redundant plugin scripts and styles that (presumably) aren&#8217;t even meant to be there.</p>
<p>After a bit of diggiing around it seems that the solution is the <code>wp_print_scripts()</code> function. So, to get jQuery working on the login form, put these lines in the code you hook onto <code>login_head</code>:</p>
<pre name="code" class="php">wp_enqueue_script( 'jquery' );
wp_print_scripts();</pre>
]]></content:encoded>
			<wfw:commentRss>http://sltaylor.co.uk/blog/adding-scripts-wordpress-login-registration-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery hover drop-down menus and the setTimeout issue</title>
		<link>http://sltaylor.co.uk/blog/jquery-hover-drop-down-menu-settimeout/</link>
		<comments>http://sltaylor.co.uk/blog/jquery-hover-drop-down-menu-settimeout/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 18:25:35 +0000</pubDate>
		<dc:creator>Steve Taylor</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://sltaylor.co.uk/?p=161</guid>
		<description><![CDATA[You know those days when you kind of know there&#8217;s plugins and whatnot aplenty to do something? But you&#8217;re feeling stubborn, and you&#8217;d rather re-invent the wheel? Today has been like that. It&#8217;s not just being stubborn, though. Sometimes you want to re-invent the wheel so you know how to make better wheels yourself. In [...]]]></description>
			<content:encoded><![CDATA[<p>You know those days when you kind of know there&#8217;s plugins and whatnot aplenty to do something? But you&#8217;re feeling stubborn, and you&#8217;d rather re-invent the wheel? Today has been like that.</p>
<p>It&#8217;s not just being stubborn, though. Sometimes you want to re-invent the wheel so you know how to make better wheels yourself. In coding, you have to strike a balance between getting stuff done quickly and efficiently by relying on what&#8217;s already been done, and actually progressing your own skills.</p>
<p>Anyway, today I solved something that had been bugging me for a while: creating slick drop-down menus. I know CSS-based drop-downs, and they&#8217;re a good thing. But having got into <a href="http://docs.jquery.com/Main_Page">jQuery</a> recently, I wanted to get them right with scripting.</p>
<p><span id="more-161"></span></p>
<p>The main trick I&#8217;d missed was how to make sure that if the cursor passes quickly over a menu item, the &#8220;show menu&#8221; bit doesn&#8217;t get triggered&#8212;preventing shoddy, jittery menus popping up all over as you reach for the page scroller or something.</p>
<p>Well, after a few hours&#8217; wrangling, here&#8217;s the code:</p>
<pre name="code" class="js">jQuery( document ).ready( function($) {
	var navTimers = [];
	$( "#nav-main ul.nav &gt; li" ).hover(
		function () {
			var id = jQuery.data( this );
			var $this = $( this );
			navTimers[id] = setTimeout( function() {
				$this.children( 'ul' ).fadeIn( 300 );
				navTimers[id] = "";
			}, 300 );
		},
		function () {
			var id = jQuery.data( this );
			if ( navTimers[id] != "" ) {
				clearTimeout( navTimers[id] );
			} else {
				$( this ).children( "ul" ).fadeOut( 200 );
			}
		}
	);
});</pre>
<p>A few words of explanation:</p>
<p>The <code>navTimers</code> array is crucial. The key to all this is storing a separate <a href="http://www.w3schools.com/htmldom/met_win_settimeout.asp"><code>setTimeout()</code></a> timer reference for each menu element that the cursor goes over.</p>
<p>OK, so <code>#nav-main ul.nav > li</code> selects the immediate children of my main nav menu list, and then I set up the <a href="http://docs.jquery.com/Events/hover">hover</a> functions.</p>
<p>The first line in each hover function uses the jQuery <a href="http://docs.jquery.com/Internals/jQuery.data#elem"><code>data()</code></a> function, which returns a unique ID for the current element (just an integer, which seems to be related to the order of the elements). We use that as our key for the <code>navTimers</code> array.</p>
<p>In the &#8220;over&#8221; function, we create a reference to the jQuery object for the current element, so we can pass it through to the function inside <code>setTimeout()</code>. Then when initialize the <code>setTimeout()</code> itself, the function inside it doing two things:</p>
<ol>
<li>Triggers the jQuery <code>fadeIn()</code> effect.</li>
<li>Clears its own reference in <code>navTimers</code>. (Actually I&#8217;m not sure if this is cleared automatically after the timer expires&#8212;I did this just to be safe.)</li>
</ol>
<p>That 300 at the end of the <code>setTimeout()</code> is the number of milliseconds it&#8217;ll wait before triggering the <code>fadeIn()</code>. The 300 <em>inside</em> the <code>fadeIn()</code> is the number of milliseconds jQuery will take to do the fade.</p>
<p>The &#8220;out&#8221; function checks if there&#8217;s a timer set for the current element. If there is, it just clears it. This is the eventuality where the cursor has gone over and then out of the element&#8217;s space within our set limit (300 ms). If there isn&#8217;t a timer, i.e. the timer&#8217;s expired and the drop-down&#8217;s been shown, then it hides the drop-down.</p>
<p>Whew! Do let me know if you spot any issues, or ways of doing things better.</p>
<p>Oh, if you&#8217;re here looking for a ready-made drop-down menu solution, including markup and CSS&#8230; apologies, but this post is to help out other who are being stubborn and a re-inventing the wheel ;-). But really, if you can&#8217;t find a good plugin (I&#8217;m working within the WordPress world here by the way), the markup and CSS are the easy bits. Hopefully the above helps out with the tricky JavaScript.</p>
]]></content:encoded>
			<wfw:commentRss>http://sltaylor.co.uk/blog/jquery-hover-drop-down-menu-settimeout/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
