<?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>LDIR: LoaD_Increment_Repeat &#187; javascript</title>
	<atom:link href="http://blog.peter-ryan.co.uk/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.peter-ryan.co.uk</link>
	<description>The (mostly techie) ramblings of Peter Ryan.</description>
	<lastBuildDate>Wed, 18 May 2011 17:17:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using CSS3 :target selectors</title>
		<link>http://blog.peter-ryan.co.uk/2008/03/14/using-css3-target-selectors/</link>
		<comments>http://blog.peter-ryan.co.uk/2008/03/14/using-css3-target-selectors/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 00:52:43 +0000</pubDate>
		<dc:creator>Peter Ryan</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.peter-ryan.co.uk/2008/03/14/using-css3-target-selectors/</guid>
		<description><![CDATA[This is a quick post to present a JavaScript that provides CSS3 :target pseudo selector like functionality for browsers that do not provide native :target support. function() { var cssClass = 'target'; // Initialise targeted fragments if any. var target = document.getElementById(window.location.hash.slice(1)); if (target) { target.className += ' ' + cssClass; } // Click event [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick post to present a JavaScript that provides CSS3 :target pseudo selector like functionality for browsers that do not provide native :target support.</p>
<pre name="code" class="javascript:nogutter:nocontrols:firstline[1]">function() {
    var cssClass = 'target';

    // Initialise targeted fragments if any.
    var target = document.getElementById(window.location.hash.slice(1));
    if (target) {
        target.className += ' ' + cssClass;
    }

    // Click event handler function generator. This is passed a "frag" argument
    //   that is a reference to the target element.
    var reCSS = new RegExp("\\b" + cssClass + "\\b");
    function newTargetClickHandler(frag) {
        return function() {
            if (target) {
                target.className = target.className.replace(reCSS, '');
            }
            target = frag;
            target.className += ' ' + cssClass;
        }
    }

    // Run thru &lt;a&gt; elements and add an onclick handler to those that reference
    //   document fragments within the current page.
    var reURI = new RegExp('^' + window.location.href.match(/^[^#]+/)[0] + '#([^#]+)$');
    var elems = document.getElementsByTagName('a');
    for (var i=0; i&lt;elems.length; i++) {
        if (elems[i].href &amp;&amp; elems[i].href.match(reURI)) {
            elems[i].onclick = newTargetClickHandler(document.getElementById(RegExp.$1));
        }
    }
};</pre>
<p>When the user clicks on a link that points to a fragment within the current page, this code will add a CSS class of &#8220;target&#8221; to the targeted element, and will remove the same &#8220;target&#8221; class from any previously targeted element.</p>
<p>Originally I was using the CSS3 :target pseudo selector and the .target class selector together in the same rule set, but I found that Opera (9.26) would not apply the rules presumably because it considered it invalid.</p>
<pre name="code" class="javascript:nogutter:nocontrols:firstline[1]">/* THIS DOES NOT WORK! */
h2:target,
h2.target {
    color: red;
}</pre>
<p>I&#8217;m not sure if this is correct behavior, but regardless of cause, I found I had to use a duplicate set of CSS rules; one for both modes of selection.</p>
<pre name="code" class="javascript:nogutter:nocontrols:firstline[1]">h2:target {
    color: red;
}
h2.target {
    color: red;
}</pre>
<p>And basically&#8230; that&#8217;s it! You can <a href="http://peter-ryan.co.uk/static/20080314/example01.html">see it in action on this test page</a>, and I hope to add a follow up post soon&#8230;. time allowing.</p>
<p>You download the the JavaScript here:<br />
<a href="http://peter-ryan.co.uk/static/20080314/csstarget.js">Full-fat script with white-space and comments</a> [1621 bytes]<br />
<a href="http://peter-ryan.co.uk/static/20080314/csstarget-min.js">Minified script</a> [1101 bytes]</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.peter-ryan.co.uk/2008/03/14/using-css3-target-selectors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Suckerfish menus with hide delay</title>
		<link>http://blog.peter-ryan.co.uk/2007/12/08/suckerfish-menus-with-hide-delay/</link>
		<comments>http://blog.peter-ryan.co.uk/2007/12/08/suckerfish-menus-with-hide-delay/#comments</comments>
		<pubDate>Sat, 08 Dec 2007 19:38:40 +0000</pubDate>
		<dc:creator>Peter Ryan</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[suckerfish]]></category>

		<guid isPermaLink="false">http://blog.peter-ryan.co.uk/2007/12/08/test-post/</guid>
		<description><![CDATA[You&#8217;re using Suckerfish Dropdown menus but you would like to prevent the dropdown menus from disappearing the moment the users mouse steps 1 pixel outside the dropdown? This question is especially relevant with multi-level dropdown menus; multi-level menus and laptop track-pads are something I&#8217;ll never get used to! The code presented below is designed to [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re using <a href="http://www.htmldog.com/articles/suckerfish/dropdowns/">Suckerfish Dropdown menus</a> but you would like to prevent the dropdown menus from disappearing the moment the users mouse steps 1 pixel outside the dropdown?</p>
<p>This question is especially relevant with multi-level dropdown menus; multi-level menus and laptop track-pads are something I&#8217;ll never get used to!</p>
<p>The code presented below is designed to be a drop-in replacement for the standard suckerfish Javascript. The original code was only required to make <acronym title="Microsoft Internet Explorer">MSIE</acronym> work correctly, the replacement code targets all browsers. If Javascript has been disabled by the user, then the CSS will function as before.</p>
<pre>sfHover = function() {
	var timeout = 600;
	var cssClass = "sfhover";

	var queue = [];
	var reCSS = new RegExp("\\b" + cssClass + "\\b");
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i&lt;sfEls.length; i++) {

		// mouseover and mouseout handlers for regular mouse based interface.
		sfEls[i].onmouseover = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onmouseout = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// focus and blur handlers for keyboard based navigation.
		sfEls[i].onfocus = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onblur = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// click event handler needed for tablet type interfaces (e.g. Apple iPhone).
		sfEls[i].onclick = function(e) {
			if (this.className.search(reCSS) == -1) {
				// CSS not set, so clear all sibling (and decendants) menus, and then set CSS on this menu...
				var elems = this.parentNode.getElementsByTagName("li");
				for (var i=0; i&lt;elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className += " " + cssClass;
			} else {
				// CSS already set, so clear all decendant menus and then this menu...
				var elems = this.getElementsByTagName("li");
				for (var i=0; i&lt;elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className = this.className.replace(reCSS, "");
			}
			if (e &amp;&amp; e.stopPropagation)
				e.stopPropagation();
			else
				window.event.cancelBubble = true;
		}
	}

	queueFlush = function () {
		while (queue.length) {
			clearTimeout(queue[0][0]);
			queueTimeout();
		}
	}

	queueTimeout = function() {
		if (queue.length) {
			var el = queue.shift()[1];
			el.className = el.className.replace(reCSS, "");
		}
	}
}
addLoadEvent(sfHover);</pre>
<p>The code also includes handlers for <code>focus</code> and <code>blur</code> events, to allow site visitors to <em>tab</em> through the menus without using a mouse.</p>
<p>And finally, a <code>click</code> event handler is added to parent menus so that they can be opened on devices that don&#8217;t support hover events such as the iPhone/iPod Touch. This might cause a problem if your parent menu item is also a link however!</p>
<p>Oh&#8230; and finally finally, the above code calls the an <code>addLoadEvent()</code> function to force <code>sfHover</code> to run on page load. If you don&#8217;t already have a similar function, here&#8217;s one you can use:</p>
<pre>function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}</pre>
<p>UPDATE 2008-05-19: I have uploaded a <a href="http://peter-ryan.co.uk/static/20071208/example01.html">very basic example page here</a>, and the <a href="http://peter-ryan.co.uk/static/20071208/sfdelay.js">JavaScript is available for download here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.peter-ryan.co.uk/2007/12/08/suckerfish-menus-with-hide-delay/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

