<?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>Baron Knoxburry &#187; javascript</title>
	<atom:link href="http://b-knox.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://b-knox.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 03 May 2012 04:41:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Detect an Empty Set in jQuery</title>
		<link>http://b-knox.com/181/detect-an-empty-set-in-jquery/</link>
		<comments>http://b-knox.com/181/detect-an-empty-set-in-jquery/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 21:31:11 +0000</pubDate>
		<dc:creator>b-knox</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://b-knox.com/?p=181</guid>
		<description><![CDATA[I spent over a half hour looking for the best solution to this.  Personally, I blame the jQuery documentation.  When reading over the jQuery core description it states, starting in version 1.4, that jQuery returns an empty set but offers no method to detect it.  Ultimately, I found that .length is the way to go [...]]]></description>
			<content:encoded><![CDATA[<p>I spent over a half hour looking for the best solution to this.  Personally, I blame the jQuery documentation.  When reading over the <a href="http://api.jquery.com/jQuery/">jQuery core</a> description it states, starting in version 1.4, that jQuery returns an empty set but offers no method to detect it.  Ultimately, I found that <a href="http://api.jquery.com/length/">.length</a> is the way to go but I wanted to expound on all three methods I discovered.</p>
<h3>count with javascript</h3>
<p><code>function jQueryCount(obj) {<br />
  var len = 0;<br />
  for (var k in obj)<br />
    len++;<br />
  return len;<br />
}</code></p>
<p>This was the first type of solution I found.  Actually, I haven&#8217;t even tried it.  I knew right off the bat there has to be a built in jQuery function so I kept digging&#8230;</p>
<h3>.size()</h3>
<p><code>if ($('ul li:visible').size()==0) {<br />
  /* do stuff here if no visible li elements exist */<br />
}</code></p>
<p>Yes!  After scouring the API documentation for 20 minutes I finally stumbled upon what I was looking for — <a href="http://api.jquery.com/size/">.size()</a> It returns a count of elements inside a jQuery object.  If there are no elements in the object it returns zero.  </p>
<p>But what&#8217;s this?!?!  The comments on the entry say it&#8217;s a stupid function that should be deprecated.  The page suggests something better&#8230;</p>
<h3>.length</h3>
<p><code>if ($('ul li:visible').length==0) {<br />
  /* do stuff here if no visible li elements exist */<br />
}</code></p>
<p><a href="http://api.jquery.com/length/">.length</a> works in a different way to get a similar answer.  It seems to be &#8220;faster, and more consistent to JavaScript in general&#8221;.  I hope this is helpful to someone in a similar situation!  :D</p>
]]></content:encoded>
			<wfw:commentRss>http://b-knox.com/181/detect-an-empty-set-in-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>April Fools&#8217; Pranks with Javascript and jQuery</title>
		<link>http://b-knox.com/127/april-fools-pranks-with-javascript-and-jquery/</link>
		<comments>http://b-knox.com/127/april-fools-pranks-with-javascript-and-jquery/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 20:06:13 +0000</pubDate>
		<dc:creator>b-knox</dc:creator>
				<category><![CDATA[Web Tutorial]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prank]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://b-knox.com/?p=127</guid>
		<description><![CDATA[Tomorrow is April Fools&#8217; Day and I thought, &#8220;This year, Imma gonna do-a sumptin!&#8221;  Who am I going to prank?  Battle of the Bits of course!  I don&#8217;t think those noobs even know I have this blog.  :/ So&#8230; to get started&#8230;  we need to detect that it is indeed April 1st, the day of [...]]]></description>
			<content:encoded><![CDATA[<p>Tomorrow is April Fools&#8217; Day and I thought, &#8220;This year, Imma gonna do-a sumptin!&#8221;  Who am I going to prank? <a href="http://battleofthebits.org"> Battle of the Bits</a> of course!  I don&#8217;t think those noobs even know I have this blog.  :/</p>
<p>So&#8230; to get started&#8230;  we need to detect that it is indeed April 1st, the day of April Fools&#8217;.  This can be done with some simple javascript —</p>
<p><code>var today = new Date();<br />
if (today.getMonth()+'.'+today.getDate()=='3.1')  {<br />
/*  put your pranks in HERE!!  :D  */<br />
}<br />
</code></p>
<p>At first glance this looks screwy.  We create a Date() object and get today&#8217;s month and day.  But April 1st != 3.1 &#8211; it should equal 4.1 right?  Silly javascript!  Month values are stored [0..11] instead of one through twelve.  I thought maybe the day would be messed up too as it is stored [0..31] but it works correctly.  I tested this code by setting my operating systems clock ahead to 4/1.  The neat thing about using javascript for the date check is that it uses the visitors&#8217; system clock rather than the servers.</p>
<h3>Let&#8217;s add a prank : Sound</h3>
<p>And for my first prank I will add a sound to every link click.  This sound happens to be my own voice exclaiming *KLINK*.  Here I add some jQuery, using the .live() method on all anchor tags.</p>
<p><code>$(document).ready(function(){<br />
  $('a').live('click',<br />
    function(event) {<br />
      event.preventDefault();<br />
      $('#footer').before('<embed src="/klink.wav" hidden="true" autostart="true"/>');<br />
      if (!$(this).hasClass('updateAjaxContent')&amp;&amp;!$(this).hasClass('speakerPopout'))<br />
      window.setTimeout('window.location="'+$(this).attr('href')+'"',500);<br />
    }<br />
  );<br />
});</code></p>
<p>This code is placed inside the date check so it will only run on April Foools&#8217; Day.  First, we prevent the default event from the click.  Then we insert a hidden &lt;embed&gt; tag that autoplays our sound.  The visitor&#8217;s browser should know what to do, automajikally throwing a Windows Media or Quicktime player into the page depending on browser settings.  If you don&#8217;t have a #footer, make sure to change that to a place that exists on your page.  Finally, I run a check on the link&#8217;s classes to make sure they don&#8217;t already have javascript functionality.  If they do, the sound just plays.  If they don&#8217;t, we give the browser a half second to play the sound and then move on to the next page.</p>
<h3>Another prank : Color</h3>
<p>Want to really confuse your visitors?  Let&#8217;s shift the colors of your layout randomly!  Once again I use some jQuery, embed the following code inside the $(document).ready() which is inside the date check.</p>
<p><code>function dec2hex(a) {<br />
      a = parseInt(a).toString(16);<br />
      return a.length&lt;2?"0"+a:a;<br />
    }<br />
<br />
    var swatch, r, g, b, color;<br />
<br />
    randomColor();<br />
<br />
    function randomColor()  {<br />
      swatch = Math.floor(Math.random()*5);<br />
      r = dec2hex(Math.floor(Math.random()*256));<br />
      g = dec2hex(Math.floor(Math.random()*256));<br />
      b = dec2hex(Math.floor(Math.random()*256));<br />
      color = '#'+r+g+b;<br />
      switch (swatch) {<br />
        case 0:<br />
          $('boxLink:hover').css('background-color',color);<br />
          $('body').animate({color:color},2500,function(){randomColor()});<br />
          break;<br />
        case 1:<br />
          $('.boxLink').css('color',color);<br />
          $('a').animate({color:color},12500,function(){randomColor()});<br />
          break;<br />
        case 2:<br />
          $('.logo1').animate({color:color},3500,function(){randomColor()});<br />
          break;<br />
        case 3:<br />
          $('.logo0').animate({color:color},1500,function(){randomColor()});<br />
          break;<br />
        case 4:<br />
          $('html').css('background-color',color);<br />
          $('body').css('background-color',color);<br />
          $('boxLink:hover').animate({color:color},4500,function(){randomColor()});<br />
          break;<br />
      }<br />
    }</code></p>
<p>I removed over half of the elements I am affecting with the randomly generated colors to make this easier to understand.  The variable &#8216;swatch&#8217; is used to identify which color is being affected in the layout.  dec2hex() helps convert the random values to a hex color value.  I&#8217;m using the callback feature of the .animate() function to keep the colors fluxing (though I don&#8217;t see any color fading to another color {a bug?!?!}).  If you call randomColor() directly from inside itself the browser will hang or you&#8217;ll get a recursive script error.</p>
<h3><a href="http://b-knox.com/tut_demos/BotB_AprilFools/Battle%20of%20the%20Bits.htm">View Demo Here</a></h3>
<h3><a href="http://b-knox.com/tut_demos/BotB_AprilFools/BotB_AprilFools.zip">Get Source Here</a></h3>
<p>Tested in Firefox and Chrome.  I saved a BotB render page to share the effects.  I only replaced the links in the main menu and disabled the April Fools check in jqueryAprilFools.js so you can see the prank in actions always!</p>
<p>I hope somebody learns something.  :D</p>
]]></content:encoded>
			<wfw:commentRss>http://b-knox.com/127/april-fools-pranks-with-javascript-and-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

