<?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>nFriedly Web Dev Tech Blog &#187; javascript</title>
	<atom:link href="http://nfriedly.com/techblog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://nfriedly.com/techblog</link>
	<description>Expert Advice on Website Development, Javascript, Ajax, and Security</description>
	<lastBuildDate>Wed, 16 May 2012 23:48:27 +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>How to get your apps ready for Facebook&#8217;s OAuth 2.0 upgrade</title>
		<link>http://nfriedly.com/techblog/2011/09/javascript-changes-for-facebooks-oauth-2-0-upgrade/</link>
		<comments>http://nfriedly.com/techblog/2011/09/javascript-changes-for-facebooks-oauth-2-0-upgrade/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 20:44:35 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oauth2.0]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=394</guid>
		<description><![CDATA[I just finished a writeup on the necessary JS changes to support Facebook&#8217;s OAuth 2.0 upgrade, and then Hilary did a followup post on the server-side. The upgrade enabled right now, but it gets forced out on October 1st. These two posts should give you all you need to know to get your site ready [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" src="http://oauth.net/images/oauth-2-sm.png" alt="" width="124" height="123" style="margin-bottom: 0;"/>I just finished a writeup on the necessary JS changes to support Facebook&#8217;s OAuth 2.0 upgrade, and then Hilary did a followup post on the server-side. </p>
<p>The upgrade enabled right now, but it gets forced out on October 1st. These two posts should give you all you need to know to get your site ready for Facebook&#8217;s upgrade:</p>
<p>Part 1: JavaScript - <a href="http://blog.sociablelabs.com/2011/09/16/javascript-changes-facebook-pauth-2-0-upgrade/">JavaScript changes for Facebook’s OAuth 2.0 upgrade</a></p>
<p>Part 2: PHP / Backend &#8211; <a href="http://blog.sociablelabs.com/2011/09/19/server-side-changes-facebook-oauth-2-0-upgrade/">Server Side changes for Facebook’s OAuth 2.0 upgrade</a></p>
<p>Also worth noting, there&#8217;s a lot of good information on <a href="https://developers.facebook.com/blog/">Facebook&#8217;s Developer Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2011/09/javascript-changes-for-facebooks-oauth-2-0-upgrade/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Calling all (available) Web Designers &amp; Developers</title>
		<link>http://nfriedly.com/techblog/2010/12/calling-all-avaiable-web-designers-developers/</link>
		<comments>http://nfriedly.com/techblog/2010/12/calling-all-avaiable-web-designers-developers/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 17:29:24 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=329</guid>
		<description><![CDATA[Even though I&#8217;ve been happily employed with Sociable Labs for a while now, I still get fairly frequent calls and emails from people who want to know if I&#8217;m available or I &#8220;know someone.&#8221; The list of people I know is starting to run short, so this post is intended to remedy that. If you [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nfriedly.com/techblog/2010/12/calling-all-avaliable-web-designers-developers"><img class="alignright size-full wp-image-358" title="Ring Ring!" src="http://nfriedly.com/techblog/wp-content/uploads/2010/12/1171473_phone_home-min.jpg" alt="" width="200" height="245" /></a>Even though I&#8217;ve been happily employed with <a href="http://sociablelabs.com/">Sociable Labs</a> for a while now, I still get fairly frequent calls and emails from people who want to know if I&#8217;m available or I &#8220;know someone.&#8221; The list of people I know is starting to run short, so this post is intended to remedy that.</p>
<p><strong>If you are an individual Web Designer or Web Developer interested in work, please post a comment below.</strong></p>
<p><strong><span id="more-329"></span></strong>What to post:</p>
<ul>
<li>Your name and website in the appropriate fields. I disabled WordPress&#8217;s <code>rel="nofollow"</code> just for you.
<ul>
<li>If you don&#8217;t have a website, get one! Sign up for a <a href="http://www.dreamhost.com/r.cgi?225072">Dreamhost account</a> and buy a template from <a href="http://themeforest.net/?ref=nfriedly">Theme Forest</a> and you&#8217;ll have a solid website in no time.</li>
</ul>
</li>
<li>Your basic skillset. For me, it might be &#8220;Strong Javascript / Ajax, PHP, HTML, and CSS; basic Photoshop, ActionScript and Java&#8221;</li>
<li>A short bit about yourself and your experience.</li>
<li>Where you&#8217;re located, and if you would consider relocating for work.</li>
<li>What type of work you&#8217;re interested in: freelance / short-term contract (3-6 months or less), long-term contract, employment, startup, etc.</li>
</ul>
<p>Tips:</p>
<ul>
<li>This is your first impression to potential clients: Copy-paste it into Word and read it out loud once or twice. Get it right the first time because you can&#8217;t edit comments after you&#8217;ve posted them.</li>
<li>Contact information: any phone number or email address posted here will be picked up by spammers.
<ul>
<li> A basic solution is to just post a .jpg on your website with your phone number and email.</li>
<li>A better solution could be a contact form like this one: <a href="http://nfriedly.com/techblog/2009/06/how-to-build-a-spam-free-contact-forms-without-captchas/">How to build a spam-free contact form without captchas</a></li>
</ul>
</li>
<li>One or two links to your GitHub, LinkedIn, or recent work is fine, but be warned that my WordPress marks<strong> </strong>anything with <strong>more than 3 links as spam</strong>, and it&#8217;s hidden until I get around to checking the spam filter.</li>
<li>Lastly, this is my website, and I may choose to edit or delete comments. For example, if you claim 10+ years experience with jQuery, your comment will be deleted. (JQuery was released in 2006)</li>
</ul>
<p>Thanks and best wishes with your future work!</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2010/12/calling-all-avaiable-web-designers-developers/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>How Facebook sets and uses cross-domain cookies</title>
		<link>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/</link>
		<comments>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 16:08:06 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=310</guid>
		<description><![CDATA[I&#8217;ve seen a lot of confusion about this lately, so I thought I&#8217;d make a quick writeup to explain how facebook does it. (I&#8217;ll also give a quick tip on how you can do it yourself.) What Facebook Does Facebook is in a unique position compared to many developers looking to set cross domain cookies: [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" src="http://nfriedly.com/img/portfolio/facebook.png" alt="" />I&#8217;ve seen a lot of confusion about this lately, so I thought I&#8217;d make a quick writeup to explain how facebook does it. (I&#8217;ll also give a quick tip on how you can do it yourself.)</p>
<h3>What Facebook Does</h3>
<p>Facebook is in a unique position compared to many developers looking to set cross domain cookies: The user visits both facebook.com and the other website. <span id="more-310"></span></p>
<p>Facebook never actually sets cookies cross-domain, they only read cookies cross-domain. They set cookies on facebook.com when the user visits facebook.com and they set cookies on the other  example.com (or any other website) when the user visits example.com.<sup><a href="#note-1">1</a></sup></p>
<p>Doing things this way avoids all of the browser security issues because cookies that were already set when the user visited facebook.com can still be read when example.com loads facebook.com in an iframe. This is worth repeating: <strong>Cookies can be read in an iframe if they were set outside of the iframe</strong>.<sup><a href="#note-2">2</a></sup></p>
<h3>What about when the user is not logged into Facebook?</h3>
<p><strong>(This is how you can do it!)</strong></p>
<p>If the user is not logged into Facebook when trying to use Facebook on example.com, then Facebook opens a popup window &#8211; not an iframe &#8211; to let the user log in.</p>
<p>A popup window has none of the cookie restrictions that iframes get; it can read and set cookies normally.</p>
<h3>What about popup blockers?</h3>
<p>Most popup blockers make a special exception for &#8220;intentional&#8221; popups &#8211; ones that occur as a direct result of a user&#8217;s click. When the user clicks the login button, the blocker allows the popup because the click indicates that the user wanted that popup.</p>
<h3>An alternate method for of cross-domain cookies: flash</h3>
<p>If you&#8217;re looking for a flash-based method of setting cross-domain cookies, or would otherwise like to avoid popups, you may want to check out my previous article, which includes source code: <a href="http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/">.swf for JavaScript cross-domain flash cookies</a></p>
<h3>Notes</h3>
<ol>
<li><a name="note-1"></a>Cookies are only set on example.com when using the using <a href="http://developers.facebook.com/docs/reference/javascript/">Facebook&#8217;s JavaScript SDK</a>. When embedding Social plugins directly as an iframe, only facebook.com cookies are used.</li>
<li><a name="note-2"></a>Safari sometimes prevents JavaScript from reading cookies in an iframe even if GET and POST requests to the server have full access to the cookies. Safari has several quirks like this, but generally <a href="http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/">behaves better with iframes if the user <em>interacts</em> with it</a>.</li>
</ol>
<h2>Need a more advanced integration than what Facebook Social Plugins provide?</h2>
<p>At <a href="http://sociablelabs.com">Sociable Labs</a>, our Intelligent Social Plugins<sup>TM</sup> increase social sharing by 15x and have shown a ~1% increase in sales. And the best part is that <a href="http://www.sociablelabs.com/product.html">we do all of the hard work for you!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript library and .swf for cross-domain flash cookies</title>
		<link>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/</link>
		<comments>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 18:25:11 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[actionscript 3.0]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[externalinterface]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flashcookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[sharedobject]]></category>
		<category><![CDATA[swf]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=272</guid>
		<description><![CDATA[I&#8217;m working on a project that has a legitimate (non-spammy) reason to need cross-domain cookies, and we settled on flash as a good way to accomplish this. However, I was surprisingly unable to find any complete library or how-to guide for connecting flash cookies to javascript. So I dusted off my flash skills and built [...]]]></description>
			<content:encoded><![CDATA[<div style="position:relative;">
<img class="alignright size-full wp-image-295" title="325990_chocolate_chip_cookies_2" src="http://nfriedly.com/techblog/wp-content/uploads/2010/07/325990_chocolate_chip_cookies_2.jpg" alt="" width="300" height="224" />I&#8217;m working on a project that has a legitimate (non-spammy) reason to need cross-domain cookies, and we settled on flash as a good way to accomplish this.</p>
<p>However, I was surprisingly unable to find any complete library or how-to guide for connecting flash cookies to javascript. So I dusted off my flash skills and built one, and and now you get to enjoy the fruit of my labor:</p>
<p><a href="http://github.com/nfriedly/Javascript-Flash-Cookies"><img style="position: absolute; top: 0pt; right: 0pt; border: 0pt none;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
</div>
<p><span id="more-272"></span></p>
<p><a href="http://github.com/nfriedly/Javascript-Flash-Cookies/zipball/master"><img class="alignleft" src="/img/application_put.png" alt="" /> Download the .swf, .js, and source code from github</a></p>
<p>This is an .swf file  that communicates with JavaScript via flash&#8217;s <a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html">ExternalInerface</a> to read and write to a Local <a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html">SharedObject</a> (LSO). Essentially, it&#8217;s cross-domain cookies for javascript.</p>
<p>It also includes an (optional) javascript library that handles embedding, communication, error checking, and logging.</p>
<p>The project is hosted at github: <a href="http://github.com/nfriedly/Javascript-Flash-Cookies">http://github.com/nfriedly/Javascript-Flash-Cookies</a></p>
<hr style="margin-top: 20px;" />
<p><em>You might also be interested in <a href="http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/">How Facebook Sets and uses cross-Domain cookies</a></em></p>
<hr />
<h3>Working Example</h3>
<p>See <a href="http://nfriedly.com/stuff/swfstore-example/">https://nfriedly.com/stuff/swfstore-example/</a> and <a href="http://nfriedly.github.com/Javascript-Flash-Cookies/">http://nfriedly.github.com/Javascript-Flash-Cookies/</a> for a working example. </p>
<h3>Quick start guide</h3>
<p>To use the library, upload the storage.swf &amp; swfstore.js files to your web server and put this HTML and JavaScript into your web page(s):</p>
<h4>The HTML</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;!-- This example uses jquery, but SwfStore does not require jquery to work. --&gt;
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/PATH/TO/swfstore.js&quot;&gt;&lt;/script&gt;

&lt;input id=&quot;dataInput&quot; /&gt; &lt;input id=&quot;saveBtn&quot; type=&quot;submit&quot; value=&quot;Save&quot; /&gt;

&lt;div id=&quot;status&quot;&gt;&lt;/div&gt;
</pre>
<h4>And The JavaScript</h4>
<pre class="brush: jscript; title: ; notranslate">

// wait until the page has finished loading before starting
$(function(){

	// first disable things while the swfStore is initializing
	$('input').attr(&quot;disabled&quot;,&quot;disabled&quot;);
	$('#status').text('Loading...');

	var mySwfStore = new SwfStore({

		// Optional but recommended. Try to choose something unique.
		namespace: 'myExample', 

		// To work cross-domain, only one of your sites should have the
		// .swf, all other sites should load it from the first one
		swf_url: 'http://site.com/PATH/TO/storage.swf', 

		// Logs messages to the console if available, a div at the
		// bottom of the page otherwise.
		debug: true,

		onready: function(){
			// Now that the swfStore was loaded successfully, re-enable
			$('input').removeAttr(&quot;disabled&quot;);

			// Read the existing value (if any)
			$('#dataInput').val(mySwfStore.get('myKey'));

			// Set up an onclick handler to save the text to the
			// swfStore whenever the Save button is clicked
			$('#saveBtn').click(function(){
				mySwfStore.set('myKey', $('#dataInput').val() );
				$('#status').text('Saved!')
			});

			$('#status').text('Loaded');
		},

		onerror: function(){
			// In case we had an error. (The most common cause is that
			// the user disabled flash cookies.)
			$('#status').text('Error');
		}
	});
});
</pre>
<h3>Cross-domain usage</h3>
<p>A copy of storage.swf located on one domain may be embedded on pages from one or more other domains, allowing cross-domain cookie access.</p>
<style>
.highlight { background-color: #D1FCA9; }
</style>
<h3>Troubleshooting</h3>
<ul>
<li>Be sure the <span class="highlight">urls</span> to the .swf file and .js file are both correct.</li>
<li>If the .swf file is unable to communicate with the JavaScript, it will <span  class="highlight">display log messages on the flash object</span>. If debug is enabled, this this should be visible on the page.</li>
<li>To <span class="highlight">hide the flash object</span> and disable the <span class="highlight">log messages appending to the bottom of the page</span>, set debug to false</span>. (Log messages are added to a div if no console is found).</li>
<li>If the user does not have flash installed, the onerror function will be called after a (configurable) 10 second timeout. You may want to use a library such as <a href="http://www.featureblend.com/javascript-flash-detection-library.html">Flash Detect</a> to check for this more quickly. <span  class="highlight">Flash Player 9.0.31.0</span> or newer is required.</li>
<li>If you pass a <span class="highlight">non-string data</span> as the key or value, things may break. Your best bet is to use strings and/or use <a href="http://json.org">JSON</a> to encode objects as strings.</li>
<li>If you see the error <span class="highlight">&#8220;uncaught exception: Error in Actionscript. Use a try/catch block to find error.&#8221;</span>, try using &#8220;//&#8221; in the .swf URL rather than &#8220;https://&#8221;. See <a href="https://github.com/nfriedly/Javascript-Flash-Cookies/issues/14">https://github.com/nfriedly/Javascript-Flash-Cookies/issues/14</a> for more information.</li>
<li>Do not set <code class="highlight">display:none</code> on the swf or any of it&#8217;s parent elements, this will cause the file to not render and the timeout will be fired. Disable debug and it will be rendered off screen.</li>
<li>The error <code class="highlight">this.swf.set is not a function</code> has been known to occur when the FlashFirebug plugin is enabled in Firefox / Firebug..
</ul>
<h3>Patches</h3>
<p>Although my JS is solid, my Flash / ActionScript skills leave something to be desired. Patches to either are more than welcome at <a href="http://github.com/nfriedly/Javascript-Flash-Cookies">github</a> (preferred), or just leave a comment here if you&#8217;re not sure how to use github. (This comment has a <a href="http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/comment-page-1/#comment-2531">short walk through to using github</a>.)</p>
<h3>Production Use</h3>
<p>If you&#8217;re using SwfStore in a production site, feel free to leave a comment here with a link to the site. I turned off WP&#8217;s default rel=&#8221;nofollow&#8221;, so enjoy the link juice <img src='http://nfriedly.com/techblog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Reciprocal links are not required, but are always appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>How to build a spam-free contact form without captchas</title>
		<link>http://nfriedly.com/techblog/2009/11/how-to-build-a-spam-free-contact-forms-without-captchas/</link>
		<comments>http://nfriedly.com/techblog/2009/11/how-to-build-a-spam-free-contact-forms-without-captchas/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 05:12:53 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[antispam]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=23</guid>
		<description><![CDATA[Most anti-spam methods used by websites today are annoying at best. They use impossible-to-read captcha images, or they make users jump through some kind of hoop to get the email address instead of just clicking on it. This can mean lost sales and opportunities for you, because each hurdle turns away more users. This article [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-39" title="data_security_3" src="http://nfriedly.com/techblog/wp-content/uploads/2009/06/data_security_3-150x150.jpg" alt="data_security_3" width="150" height="150" />Most anti-spam methods used by websites today are annoying at best. They use impossible-to-read captcha images, or they make users jump through some kind of hoop to get the email address instead of just clicking on it. <strong>This can mean lost sales and opportunities for you, because each hurdle turns away more users. </strong></p>
<p>This article looks at how to use some simple HTML, CSS, &amp; Javascript to protect your private information without making your guests jump through hoops.</p>
<p><span id="more-23"></span><br />
<a href="http://nfriedly.com/stuff/spam-free-contact.zip"><img class="alignleft" src="/img/application_put.png" alt="" /> Download a working copy of the contact form discussed here.</a></p>
<h2>The Goal</h2>
<p>I want users to be able to contact me simple and easy, no captchas, no math problems, just a regular contact form, clickable email address, and everything copy-paste-able.</p>
<h2>The Problem</h2>
<p>Spammers love captcha-free forms and clickable email addresses. (And lately, copy-paste-able phone numbers.) I do not want to receive a ton of spam!</p>
<h2>The Solution</h2>
<p>With a little bit of CSS and JavaScript wizardry, we can make a simple, easy-to-use contact page that will block almost all automated contact form spam.</p>
<h3>Part 1: The Contact Form</h3>
<p>We are going to make a standard contact form with one extra feature: an input named &#8220;url&#8221; and a note beside it that says &#8220;Don&#8217;t type anything here!&#8221;</p>
<p>The HTML:</p>
<pre class="brush: xml; title: ; notranslate">&lt;form method=&quot;post&quot; action=&quot;/submit.php&quot;&gt;
&lt;p&gt;Your name:
&lt;br /&gt;&lt;input name=&quot;name&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Your email:
&lt;br /&gt;&lt;input name=&quot;email&quot; /&gt;&lt;/p&gt;

&lt;p class=&quot;antispam&quot;&gt;Leave this empty:
&lt;br /&gt;&lt;input name=&quot;url&quot; /&gt;&lt;/p&gt;

&lt;textarea name=&quot;message&quot;&gt;&lt;/textarea&gt;

&lt;input type=&quot;submit&quot; value=&quot;Send&quot; /&gt;
&lt;/form&gt;</pre>
<p>Then we use CSS to hide the input and the note.</p>
<p>The CSS:</p>
<pre class="brush: css; title: ; notranslate">.antispam { display:none;} </pre>
<p>Then we make a rule in the server that says &#8216;if the user typed anything in the &#8220;url&#8221; box, then throw it out.&#8217;</p>
<p>The PHP:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php

// if the url field is empty
if(isset($_POST['url']) &amp;&amp; $_POST['url'] == ''){

	// then send the form to your email
	mail( 'you@yoursite.com', 'Contact Form', print_r($_POST,true) );
}

// otherwise, let the spammer think that they got their message through

?&gt;

&lt;h1&gt;Thanks&lt;/h1&gt;
&lt;p&gt;We'll get back to you as soon as possible&lt;/p&gt;</pre>
<p>A regular person won&#8217;t even see the box normally, and will therefore leave it blank without even thinking about it. If the CSS fails to load, they get a note explaining what to do.</p>
<p>However, when a spam bot looks at this, it sees a good spot to stick whatever spammy url they&#8217;re trying to advertise.</p>
<p>Now the php script on the server can tell who is a spammer and who isn&#8217;t. The regular people get sent to your email, the spammers get ignored!</p>
<h3>Part 2:  Click-able Email Address</h3>
<p>Spammers steal your email address by scanning through the source code of the site and grabbing anything that looks like an email address. So we&#8217;re going to make sure that there is no email address in the source code and instead generate it by JavaScript.</p>
<p>The Javascript:</p>
<pre class="brush: jscript; title: ; notranslate">var first = &quot;yourname&quot;;
var last = &quot;yoursite.com&quot;;</pre>
<p>The HTML:</p>
<pre class="brush: xml; title: ; notranslate">&lt;p&gt;My e-mail address:
&lt;script type=&quot;text/javascript&quot;&gt;
document.write('&lt;a href=&quot;mailto:'+first + '@' + last+'&quot;&gt;'+first + '@' + last+'&lt;\/a&gt;');
&lt;/script&gt;
&lt;noscript&gt;
Please enable javascript or use my &lt;a href=&quot;/contact.php&quot;&gt;contact form&lt;/a&gt;
&lt;/noscript&gt;
&lt;/p&gt;</pre>
<p>A regular user will see a regular email address and things just work. A user who happens to have javascript disabled will see an explanation and an alternative solution. And a spammer won&#8217;t see a thing!</p>
<p>This method can easily be extended to phone numbers and other personal information.</p>
<h2>Advanced version: Prettier message body and a proper <code>From:</code> field</h2>
<p>These were the most commonly requested features, so I added an advanced version that changes the <code>From:</code> field of the email to whatever the user typed in the box, and removes all of the <code>Array</code> brackets from the body of the message:</p>
<pre class="brush: php; title: ; notranslate">&lt;?php

// if the url field is empty
if(isset($_POST['url']) &amp;&amp; $_POST['url'] == ''){

	// put your email address here
	$youremail = 'you@yoursite.com';

	// prepare a &quot;pretty&quot; version of the message
	$body = &quot;This is the form that was just submitted:
	Name:  $_POST[name]
	E-Mail: $_POST[email]
	Message: $_POST[message]&quot;;

	// Use the submitters email if they supplied one
	// (and it isn't trying to hack your form).
	// Otherwise send from your email address.
	if( $_POST['email'] &amp;&amp; !preg_match( &quot;/[\r\n]/&quot;, $_POST['email']) ) {
	  $headers = &quot;From: $_POST[email]&quot;;
	} else {
	  $headers = &quot;From: $youremail&quot;;
	}

	// finally, send the message
	mail($youremail, 'Contact Form', $body, $headers );

}

// otherwise, let the spammer think that they got their message through

?&gt;</pre>
<p>The <code>preg_match()</code> is there to make sure spammers can&#8217;t abuse your server by injecting extra fields (such as CC and BCC) into the header. Take a look at <a href="http://www.thesitewizard.com/php/protect-script-from-email-injection.shtml">http://www.thesitewizard.com/php/protect-script-from-email-injection.shtml</a> for more info.</p>
<p>Be sure to check the comments below for several other variations.</p>
<h2>Complete Examples</h2>
<p><img class="alignleft" src="/img/application_put.png" alt="" /> A complete working copy of everything mentioned in this article, including both the simple and advanced versions, is available for download here: <a href="http://nfriedly.com/stuff/spam-free-contact.zip">http://nfriedly.com/stuff/spam-free-contact.zip</a></p>
<p>For a live demo of all of this and more, see my <a href="http://nfriedly.com/contact">contact page</a>.</p>
<h3>WordPress version</h3>
<p>I found that there is an anti-spam plugin for WordPress that uses similar methods to the ones I describe here: <a href="http://wordpress.org/extend/plugins/nospamnx/">http://wordpress.org/extend/plugins/nospamnx/</a> &#8211; I installed it on this blog and it&#8217;s stopped nearly 30,000 spam comments so far.</p>
<h2>Does your website need help?</h2>
<p>I am an <a href="/webdev">Experienced Web Developer with a sharp eye for security</a>. I can make your site easier to use while at the same time cutting down on level of spam you receive through it.  <a href="/contact">Contact me</a> for more information and a free quote.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/11/how-to-build-a-spam-free-contact-forms-without-captchas/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>Advanced Javascript: Logical Operators and truthy / falsy</title>
		<link>http://nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/</link>
		<comments>http://nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 16:54:49 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=46</guid>
		<description><![CDATA[Nearly every website on the internet uses javascript in some form or fashion. Yet very few people, even those who write it and teach it, have a clear understanding of how javascript works! Logical Operators are a core part of the language. We&#8217;re going to look at what logical operators are, what &#8220;truthy&#8221; and &#8220;falsy&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/fleur-design/308974073/"><img class="alignleft" title="speed machine" src="http://farm1.static.flickr.com/104/308974073_9057064747_m.jpg" alt="" width="240" height="160" /></a> Nearly every website on the internet uses javascript in some form or fashion. Yet very few people, even those who write it and teach it, have a clear understanding of how javascript works!</p>
<p>Logical Operators are a core part of the language. We&#8217;re going to look at what logical operators are, what &#8220;truthy&#8221; and &#8220;falsy&#8221; mean, and <strong>how to use this to write cleaner, faster and more optimized javascript</strong>.</p>
<p><span id="more-46"></span></p>
<h2>Javascript Logical Operators</h2>
<p>In traditional programming, operators such as <code>&amp;&amp;</code> and <code>|| </code> returned a boolean value (<code>true</code> or <code>false</code>). This is not the case in javascript. Here it returns the actual <code>object</code>, not a <code>true</code> / <code>false</code>.  To really explain this, I first have to explain what is truthy and what is falsy.</p>
<h3>Truthy or Falsy</h3>
<p>When javascript is expecting a <code>boolean</code> and it&#8217;s given something else, it decides whether the something else is &#8220;truthy&#8221; or &#8220;falsy&#8221;.</p>
<p>An empty string (<code>''</code>), the number <code>0</code>, <code>null</code>, <code>NaN</code>, a boolean <code>FALSE</code>, and <code>undefined</code> variables are all &#8220;falsy&#8221;. Everything else is &#8220;truthy&#8221;.</p>
<pre class="brush: jscript; title: ; notranslate">
var emptyString = &quot;&quot;; // falsy

var nonEmptyString = &quot;this is text&quot;; // truthy

var numberZero = 0; // falsy

var numberOne = 1; // truthy

var emptyArray = []; // truthy, BUT []==false is true. More below.

var emptyObject = {}; // truthy

var notANumber = 5 / &quot;tree&quot;; // falsy
// NaN is a special javascript object for &quot;Not a Number&quot;.

function exampleFunction(){
	alert(&quot;Test&quot;);
}
// examleFunction is truthy
// BUT exampleFunction() is falsy because it has no return (undefined)
</pre>
<p>Gotchas to watch out for: the strings &#8220;0&#8243; and &#8220;false&#8221; are both considered truthy.  You can convert a string to a number with the <code>parseInt()</code> and <code>parseFloat()</code> functions, or by just multiplying it by 1.</p>
<pre class="brush: jscript; title: ; notranslate">
var test = &quot;0&quot;; // this is a string, not a number

(test == false); // returns false, meaning that test is truthy

(test * 1 == false); // returns true, meaning that `test * 1` is falsy
</pre>
<p>As one commenter <a href="#comment-2100">mentioned</a>, arrays are particularly weird. If you just test it for truthyness, an empty array is truthy. HOWEVER, if you compare an empty array to a boolean, it becomes falsy:</p>
<pre class="brush: jscript; title: ; notranslate">
  if([] == false){
    // this code runs
  }

  if( [] ) {
    // this code also runs
  }

  if([] == true){
    // this code doesn't run
  }

  if( ![] ) {
    // this code also doesn't run
  }
</pre>
<p>Another commenter <a href="http://www.nicollet.net/2009/06/the-truth-of-javascript/">pointed out</a> an additional gotcha to watch out for: while javascript evaluates empty arrays as true, PHP evaluates them as false.</p>
<p>PHP also evaluates &#8220;0&#8243; as falsy. (However the string &#8220;false&#8221; is evaluated as truthy by both PHP and javascript.)</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

$emptyArray = array(); // falsy in PHP

$stringZero = &quot;0&quot;; // falsy in PHP

?&gt;
</pre>
<h3>How Logical Operators Work</h3>
<h4>Logical OR, <code>||</code></h4>
<p>The logical OR operator, <code>||</code>,  is very simple after you understand what it is doing. If the first object is truthy, that gets returned. Otherwise, the second object gets returned.</p>
<pre class="brush: jscript; title: ; notranslate">
(&quot;test one&quot; || &quot;test two&quot;); // returns &quot;test one&quot;

(&quot;test one&quot; || &quot;&quot;); // returns &quot;test one&quot;

(0 || &quot;test two&quot;); // returns &quot;Test two&quot;

(0 || false); // returns false
</pre>
<p>Where would you ever use this? The OR operator allows you to easily specify default variables in a function.</p>
<pre class="brush: jscript; title: ; notranslate">
function sayHi(name){

	var name = name || &quot;Dave&quot;;

	alert(&quot;Hi &quot; + name);

}

sayHi(&quot;Nathan&quot;); // alerts &quot;Hi Nathan&quot;;

sayHi(); // alerts &quot;Hi Dave&quot;,
// name is set to null when the function is started
</pre>
<h4>Logical AND, <code>&amp;&amp;</code></h4>
<p>The logical AND operator, <code>&amp;&amp;</code>,  works similarly.  If the first object is falsy, it returns that object. If it is truthy, it returns the second object.</p>
<pre class="brush: jscript; title: ; notranslate">
(&quot;test one&quot; &amp;&amp; &quot;test two&quot;); // returns &quot;test two&quot;

(&quot;test one&quot; &amp;&amp; &quot;&quot;); // returns &quot;&quot;

(0 &amp;&amp; &quot;test two&quot;) // returns 0
</pre>
<p>The logical AND allows you to make one variable dependent on another.</p>
<pre class="brush: jscript; title: ; notranslate">
var checkbox = document.getElementById(&quot;agreeToTerms&quot;);

var name = checkbox.checked &amp;&amp; prompt(&quot;What is your name&quot;);

// name is either their name, or false if they haven't checked the AgreeToTerms checkbox

// IMPORTANT NOTE: Internet Explorer 8 breaks the prompt function.
</pre>
<h4>Logical NOT, <code>!</code></h4>
<p>Unlike <code>&#038;&#038;</code> and <code>||</code>, the <code>!</code> operator DOES turn the value it receives into a boolean. If it receives a truthy value, it returns <code>false</code>, and if it receives a falsy value, it returns <code>true</code>.</p>
<pre class="brush: jscript; title: ; notranslate">
(!&quot;test one&quot; || &quot;test two&quot;); // returns &quot;test two&quot;
// (&quot;test one&quot; gets converted to false and skipped)

(!&quot;test one&quot; &amp;&amp; &quot;test two&quot;); // returns false
// (&quot;test one&quot; gets converted to false and returned)

(!0 || !&quot;test two&quot;); // returns true
// (0 gets converted to true and returned)
</pre>
<p>Another useful way to use the <code>!</code> operator is to use two of them &#8211; this way you always get a <code>true</code> or a <code>false</code> no matter what was given to it.</p>
<pre class="brush: jscript; title: ; notranslate">
(!!&quot;test&quot;); // returns true
//  &quot;test&quot; is converted to false, then that is converted to true

(!!&quot;&quot;); // returns false
// &quot;&quot; is converted to true, and then that true is converted to false

(!!variableThatDoesntExist); // returns false even though you're checking an undefined variable.
</pre>
<h2><a href="http://nfriedly.com/webdev">Javascript Optimization</a></h2>
<p>Need any help <a href="/webdev/javascript">optimizing the Javascript and AJAX on your website</a>? Get in touch with your friendly neighborhood <a href="http://nfriedly.com/webdev">javascript expert</a> for ideas on how to optimize your site and a free quote.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to use XSLT to style an RSS feed</title>
		<link>http://nfriedly.com/techblog/2009/06/how-to-use-xslt-to-style-an-rss-feed/</link>
		<comments>http://nfriedly.com/techblog/2009/06/how-to-use-xslt-to-style-an-rss-feed/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 19:18:39 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=191</guid>
		<description><![CDATA[XSLT is a fairly well-supported technology. It allows you to take any XML file &#8211; including RSS &#8211; and transform it into a styled HTML document. It&#8217;s kind of like CSS on steroids. Unfortunately, most browsers think they know better and go off and do their own thing on RSS feeds. We&#8217;re going to look [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/gogdog/2240855570/"><img class="alignright" src="http://farm3.static.flickr.com/2097/2240855570_4529cf9f6d_m.jpg" alt="Take this!" /></a><abbr title="Extensible Stylesheet Language Transformations">XSLT</abbr> is a fairly well-supported technology. It allows you to take any XML file &#8211; including RSS &#8211; and <em>transform</em> it into a styled HTML document. It&#8217;s kind of like CSS on steroids.</p>
<p>Unfortunately, most browsers think they know better and go off and do their own thing on RSS feeds.</p>
<p>We&#8217;re going to look at how and which browsers can be brought into line, and how to use XSLT to improve the look of your RSS feed in those browsers.</p>
<p><span id="more-191"></span></p>
<h2>The RSS problem</h2>
<p>In most browsers, XML and XSLT are supported in every single case *except* RSS. By default, Internet Explorer, Firefox, Safari, and Opera all ignore XSLT files and do their own thing with RSS. In fact, Google Chrome is the *only* browser I tested that got it right without tinkering.</p>
<p>To their credit, Microsoft at least gave their users the option to turn off the &#8220;feature&#8221;. No other browser even gives this option.</p>
<p>During my tests, I have found a way to &#8220;trick&#8221; Firefox into rendering RSS with XSLT correctly. Currently there seems to be no solution for other browsers except to try and detect them on the server and send the user an HTML file if they&#8217;re in a browser that doesn&#8217;t work properly.</p>
<h3>Internet Explorer</h3>
<p>IE requires that the user specifically choose to disable their take-liberties-with-rss &#8220;feature&#8221;. I would point out that this really isn&#8217;t good enough because 99% of users will never get that far, but sadly, it&#8217;s the closest thing to getting it right out of any browser on the market! (Aside from Google Chrome.)</p>
<p>Here&#8217;s how:</p>
<ol>
<li>Click on the <strong>Tools</strong> menu,</li>
<li>Click on the <strong>Internet Options</strong> sub-menu,</li>
<li>Click on the <strong>Content</strong> tab,</li>
<li>Click on the <strong>Settings</strong> button of the <strong>Feed</strong> section to bring up Feed Settings dialog box,</li>
<li>Un-check the <strong>Turn On Feed Reading View</strong> option.</li>
<li>Click OK all the way to close all opened dialog boxes.</li>
<li>Restart Internet Explorer</li>
</ol>
<h3><a href="http://www.flickr.com/photos/gogdog/2240065731/"><img class="alignright" src="http://farm3.static.flickr.com/2172/2240065731_86c22f48c1_m.jpg" alt="Transform!" /></a>Firefox</h3>
<p>Firefox can be tricked into working because it decides fairly early on in the rendering process whether to treat the page in a standard way or to fly off the handle with it. In fact, it makes this decision before even completely downloading the RSS file.</p>
<p>Because of the early decision process, we can insert 512 characters of white space in between the <code>&lt;?xml ?&gt;</code> declaration and the opening <code>&lt;rss&gt;</code> tag. Firefox is then &#8220;tricked&#8221; into doing the right thing and rendering the feed correctly.</p>
<h2>Working around it</h2>
<p>Although not practical in most cases currently, I&#8217;ve included an example of a script that will take any RSS feed and add a style sheet to it.  It includes the hack to work in firefox and instructions for enabling it in Internet Explorer.</p>
<p><a href="http://nfriedly.com/stuff/rss/?url=http://nfriedly.com/techblog/feed/">http://nfriedly.com/stuff/rss/?url=http://nfriedly.com/techblog/feed/</a></p>
<p>Code for index.php:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

// grab the url
if(isset($_REQUEST['url'])) $url = $_REQUEST['url'];
else $url = false;

// make sure the url is good (no local files)
if($url &amp;&amp; substr($url,0,7) != &quot;http://&quot;) exit(&quot;Please start urls with 'http://'&quot;);

// make the stylesheet link
$xsl_file = 'xsl.php';
if($url) $xsl_file .= '?url='.urlencode($url);
define('XSL_LINK','&lt;?xml-stylesheet href=&quot;'.$xsl_file.'&quot; type=&quot;text/xsl&quot; ?&gt;');

// if we don't have a url, use the home page
if(!$url) $url = &quot;home.xml&quot;;

// download the rss feed
$rss = file_get_contents($url);

// xml header so firefox doesn't decide it's text
header('content-type: text/xml');

//echo out the header right away, if there is one
if(substr($rss,0,6) == '&lt;?xml '){
	$header_end = strpos($rss,'?&gt;') +2;
	echo substr($rss,0,$header_end);
	$rss = substr($rss,$header_end);
}
//otherwise echo a default header:
else echo '&lt;?xml version=&quot;1.0&quot; ?'.'&gt;';

// remove any existing stylesheet
$rss = preg_replace('/&lt;\?xml-stylesheet([^?]|\?(?!&gt;))*\?'.'&gt;/','',$rss);  // uses lookahead

// add in our stylesheet
echo &quot;\r\n&quot; . XSL_LINK . &quot;\r\n&quot;;

// toss in 512 bytes of nothing to throw off firefox
echo &quot;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 &quot;;

//finally, pass along the content
echo $rss;

?&gt;
</pre>
<p>The xsl.php file is only php to allow for setting the current url in the feed url input box. Ignoring that, you can view it&#8217;s source by looking at <a href="http://nfriedly.com/stuff/rss/xsl.php">http://nfriedly.com/stuff/rss/xsl.php</a>. You could simply save that as an .xml file and have a working copy.</p>
<p>You can also view the <a href="http://nfriedly.com/stuff/rss/theme/style.css">CSS</a> and <a href="http://nfriedly.com/stuff/rss/scripts.js">Javascript</a> used to make everything look nice.</p>
<h2>Hire me for web development</h2>
<p>Need an <a href="http://nfriedly.com/webdev">expert web programmer</a> to research and solve some off-the-wall problem like this? I&#8217;m available. I&#8217;m good solving run-of-the-mill problems too &#8211; <a href="http://nfriedly.com/webdev/javascript">Javascript and AJAX development</a> is my specialty.  <a href="http://nfriedly.com/contact">Get in touch with me</a> for more information and a free quote.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/06/how-to-use-xslt-to-style-an-rss-feed/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>How AJAX Security and Twitter callbacks work</title>
		<link>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/</link>
		<comments>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 14:40:33 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/</guid>
		<description><![CDATA[The twitter callback feature is nice &#8211; it makes it extremely easy to to add a twitter feed to a page. But to get the most benefit out of it, you really need to understand what it&#8217;s doing. We&#8217;re going to look at how AJAX security works, specifically the Same Origin Policy, how Twitter gets [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="Breaking javascript - the right way" src="http://www.sxc.hu/pic/m/s/si/simonok/323276_game_of_pool.jpg" alt="" width="141" height="188" />The twitter callback feature is nice &#8211; it makes it extremely easy to to add a twitter feed to a page. But to get the most benefit out of it, you really need to understand what it&#8217;s doing.</p>
<p>We&#8217;re going to look at how AJAX security works, specifically the Same Origin Policy, how Twitter gets around it, and the type of callback that twitter uses.</p>
<p>Note: the callback that twitter uses is entirely different from callback in the sense of passing a javascript function around as a variable. We&#8217;ll look at that in a future article.</p>
<p><span id="more-114"></span></p>
<h2>AJAX Security</h2>
<p>The XMLHTTPRequest Object, which is the javascript object used to make AJAX requests, has a &#8220;<a href="https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript">Same Origin Policy</a>&#8221; which basically means that javascript on site1.com cannot use AJAX to directly load data from site2.com. This is a security feature, as it makes XSS (Cross-Site Scripting) attacks more difficult.</p>
<p>Worth noting, if the website is at site1.com, no scripts can communicate with any other site, even if the script was loaded from site2.com.</p>
<h2>Work Arounds</h2>
<p>There are a number of workarounds including iframes, java applets, and flash, but here&#8217;s a couple of the more common methods.</p>
<h3><img src="http://www.sxc.hu/pic/m/c/cw/cwmgary/486891_all_lined_up.jpg" alt="Line em up!" class="alignright" />Proxying Requests</h3>
<p>The way proxying works is to have a file on your server that grabs the data from a remote server and passes it along. Then for javascript, the data appears to be coming from your server, even though it actually originated at a remote server. This is what the Fancy part of my <a href="http://nfriedly.com/demos/twitter">twitter demo</a> does.</p>
<p>We&#8217;ll look at using a proxy to get remote data in a future article.</p>
<h3>Remotely hosted javascript files</h3>
<p>Scripts stored on other websites can be included on a page. As long as the script doesn&#8217;t need to call home after the initial load, everything works great. This is how a basic twitter function works: you load a script from twitter&#8217;s website and it communicates with your site via the callback feature. This is what the Simple part of my <a href="http://nfriedly.com/demos/twitter">twitter demo</a> does.</p>
<p>Here is a very basic page that uses Twitter&#8217;s callback feature and a remotely loaded javascript file to show my twitter status &#8211; remote data &#8211; on my website, by interacting with local javascript.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;  dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;

&lt;head&gt;
&lt;title&gt;Simple Twitter Status&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;My Twitter Status:&lt;/h1&gt;

&lt;div id=&quot;twitter_status&quot;&gt;Loading...&lt;/div&gt;

&lt;!-- Put scripts down here for speed --&gt;

&lt;!-- this must come before we load the twitter script --&gt;
&lt;script type=&quot;text/javascript&quot;&gt;

function showStatus(json){

	json = json[0]; // we only care about the most recent status;

	var myDiv = document.getElementById('twitter_status');

	myDiv.innerHTML = '&lt;img src=&quot;'
		+ json.user.profile_image_url
		+ '&quot; style=&quot;float:left; margin:5px 10px 10px 0&quot;&gt;'
		+ json.text;
}
&lt;/script&gt;

&lt;!-- now load the twitter file --&gt;
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://twitter.com/statuses/user_timeline/nfriedly.json?count=1&amp;amp;callback=showStatus&amp;amp;random=&lt;?php echo time(); ?&gt;&quot; /&gt;
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You can see a live copy of this code at <a href="http://nfriedly.com/demos/twitter-extra-simple">http://nfriedly.com/demos/twitter-extra-simple</a>.</p>
<h2>Digging into Twitter&#8217;s callback method</h2>
<p>Below is a trimmed down example of what Twitter&#8217;s API sends back when we make the request in the example above.</p>
<pre class="brush: jscript; title: ; notranslate">
showStatus([{&quot;in_reply_to_screen_name&quot;:null,&quot;text&quot;:&quot; [ Lots of information that I'm omitting because it's not the point. ] &quot;]);
</pre>
<p>Now, don&#8217;t worry about the jazz in the middle, just look at that showStatus(); that&#8217;s wrapped around it. First of all, how does Twitter even know that we have a function named show status? Because we said so in the url to the file -see how we added <code>&amp;callback=showStatus</code>? That&#8217;s where the magic is.  (Ok, technically we said <code>&amp;amp;</code> not just <code>&amp;</code>, but that was just to pass XHTML validation. )</p>
<p><img class="alignright" title="The break!" src="http://www.sxc.hu/pic/m/l/lj/ljweb/490307_pool_break.jpg" alt="" width="300" height="168" /></p>
<h3>Cross-domain!</h3>
<p>There&#8217;s a second important thing going on here &#8211; javascript from two different domains are interacting with each other. This is allowed because of how the Same Origin Policy works &#8211; everything is restricted to the local domain, but that means that everything can work together on the same plane.</p>
<h3>It&#8217;s a beautiful thing</h3>
<p>I hope this gave you a little bit better understanding of how AJAX security works and how Twitter gets around it and is still able to interact with your site. In the future, I&#8217;ll have an article on how &#8220;traditional&#8221; callbacks work that will use jQuery and more AJAX to dive a bit deeper into the topic.</p>
<h2>Javascript Ninja for Hire</h2>
<p>I have <a href="http://nfriedly.com/portfolio">extensive experience</a> working with AJAX, Twitter, and related technologies. I&#8217;m just the man you need to make your next <a href="http://nfriedly.com/webdev">javascript development</a> project shine!</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/06/javascript-security-ajax-json-and-twitter-callbacks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Advanced Javascript: Objects, Arrays, and Array-Like objects</title>
		<link>http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/</link>
		<comments>http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 19:45:48 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=106</guid>
		<description><![CDATA[Javascript objects and arrays are both incredibly useful. They&#8217;re also incredibly easy to confuse with each other. Mix in a few objects that look like arrays and you&#8217;ve got a recipe for confusion! We&#8217;re going to see what the differences between objects and arrays are, how to work with some of the common array-like objects, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/stawarz/2492636763/"><img class="alignleft" title="The other kind of array" src="http://farm4.static.flickr.com/3146/2492636763_788774987e_m.jpg" alt="" width="240" height="160" /></a>Javascript <code>objects</code> and <code>arrays</code> are both incredibly useful. They&#8217;re also incredibly easy to confuse with each other. Mix in a few objects that look like arrays and you&#8217;ve got a recipe for confusion!</p>
<p>We&#8217;re going to see what the differences between objects and arrays are, how to work with some of the common array-like objects, and how to get the most performance out of each.</p>
<p><span id="more-106"></span></p>
<h2>What Objects Are</h2>
<p>A javascript object is a basic data structure:</p>
<pre class="brush: jscript; title: ; notranslate">

var basicObj = {}; // an empty object
// {} is a shortcut for &quot;new Object()&quot;

basicObj.suprise= &quot;cake!&quot;;

basicObj['suprise']; // returns &quot;cake!&quot;
</pre>
<p>Using <code>{}</code> instead of <code>new Object();</code> is know as &#8220;Object Literal&#8221; syntax.</p>
<pre class="brush: jscript; title: ; notranslate">
var fancyObj = {

	favoriteFood: &quot;pizza&quot;,

	add: function(a, b){

		return a + b;
	}
};

fancyObj.add(2,3); // returns 5

fancyObj['add'](2,3); // ditto.
</pre>
<p>As you can see, and probably already knew, properties can be accessed a couple of different ways.  However, it&#8217;s an important point that we&#8217;ll come back to in a minute.</p>
<p>Everything in javascript is an <code>object</code>. Everything. <code>Arrays</code>, <code>functions</code>, even <code>numbers</code>! Because of this, you can do some really interesting things, such as modifying the <code>prototypes</code> of Objects, Arrays, etc.</p>
<pre class="brush: jscript; title: ; notranslate">

// an example of something you probably shouldn't do. Ever. Seriously.

Number.prototype.addto = function(x){

	return this + x;

}

(8).addto(9); // returns 17

// other variations:

8.addto(9); 
// gives a syntax error, because the dot is assumed to be a decimal point

8['addto'](9); 
// works but is kind of ugly compared to the first method

var eight = 8;
eight.addto(9);  // works
</pre>
<h2>What Arrays Are</h2>
<p>Javascript arrays are a type of <code>object</code> used for storing multiple values in a single variable. Each value gets  numeric index and may be any data type.</p>
<pre class="brush: jscript; title: ; notranslate">
var arr = [];  // this is a shortcut for new Array();

arr[0] = &quot;cat&quot;;
arr[1] = &quot;mouse&quot;;
</pre>
<p>See how that syntax is so similar to the syntax used for setting object properties? In fact, the only difference is that objects use a string while arrays use a number. This is why arrays get confused with objects so often.</p>
<h3>Length</h3>
<p>Arrays have a <code>length</code> property that tells how many items are in the array and is automatically updated when you add or remove items to the array. </p>
<pre class="brush: jscript; title: ; notranslate">
var arr = [];

arr[0] = &quot;cat&quot;; // this adds to the array
arr[1] = &quot;mouse&quot;; // this adds to the array
arr[&quot;favoriteFood&quot;] = &quot;pizza&quot;; // this DOES NOT add to the array
// setting a string parameter adds to the underlying object

arr.length; // returns 2, not 3
</pre>
<p>The length property is only modified when you add an item to the array, not the underlying object.</p>
<p><strong>The <code>length</code> is always 1 higher than the highest index, even if there are actually fewer items in the array.</strong></p>
<pre class="brush: jscript; title: ; notranslate">

var arr = [];

arr.length; // returns 0;

arr[100] = &quot;this is the only item in the array&quot;;

arr.length;
// returns 101, even though there is only 1 object in the array
</pre>
<p>This is somewhat counter-intuitive. PHP does more what you would expect:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

arr = array();

arr[100] = &quot;php behaves differently&quot;;

sizeof(arr); // returns 1 in PHP

?&gt;
</pre>
<p>You can manually set the <code>length</code> also. Setting it to 0 is a simple way to empty an array.</p>
<p>In addition to this length property, arrays have lots of nifty built in functions such as <code>push()</code>, <code>pop()</code>, <code>sort()</code>, <code>slice()</code>, <code>splice()</code>, and more. This is what sets them apart from Array-Like Objects.</p>
<h2><a href="http://www.flickr.com/photos/repoort/2645497916/"><img class="alignright" title="It's like an array, but made out of Lego!" src="http://farm4.static.flickr.com/3175/2645497916_386b9b75b8_m.jpg" alt="" width="240" height="180" /></a></h2>
<h2>Array-like Objects</h2>
<p>Array-like objects look like arrays. They have various numbered elements and a length property. But that&#8217;s where the similarity stops. Array-like objects do not have any of Array&#8217;s functions, and for-in loops don&#8217;t even work!</p>
<p>You&#8217;ll come across these more often than you might expect. A common one is the <code>arguments</code> variable that is present inside of every js function. </p>
<p>Also included in the category are the HTML node sets returned by <code>document.getElementsByTagName()</code>, <code>document.forms</code>, and basically every other DOM method and property that gives a list of items.</p>
<pre class="brush: jscript; title: ; notranslate">

document.forms.length; // returns 1;

document.forms[0]; // returns a form element.

document.forms.join(&quot;, &quot;); // throws a type error. this is not an array.

typeof document.forms; // returns &quot;object&quot;
</pre>
<p>Did you know you can send any number of parameters you want to a javascript function? They&#8217;re all stored in an array-like object named <code>arguments</code>.</p>
<pre class="brush: jscript; title: ; notranslate">

function takesTwoParams(a, b){

	// arguments is an  array-like variable inside of all functions
	// arguments.length works great

	alert (&quot;you gave me &quot;+arguments.length+&quot; parameters&quot;);  

	for(i=0; i&lt; arguments.length; i++){

		alert(&quot;parameter &quot; + i + &quot; = &quot; + arguments[i]); 

	}
}

takesTwoParams(&quot;one&quot;,&quot;two&quot;,&quot;three&quot;);
// alerts &quot;you gave me 3 parameter&quot;,
// then &quot;parameter 0 = one&quot;
// etc. 
</pre>
<p>This works great. But that&#8217;s about as far as you can go with array-like objects. The flowing example does not work:</p>
<pre class="brush: jscript; title: ; notranslate">

function takesTwoParams(a, b){

	alert(&quot; your parameters were &quot; + arguments.join(&quot;, &quot;));
	// throws a type error because arguments.join doesn't exist
}
</pre>
<h3>So what can you do? </h3>
<p>Well you could make your own <code>join()</code> function, but that adds a lot of unnecessary overhead to your code because it has to loop over everything. If only there were a quick way to get an array out of an array like object&#8230;</p>
<p>It turns out there is.</p>
<p>The array functions can be called on non-array objects as long as you know where to find the function (usually they&#8217;re attached to the array, but this isn&#8217;t an array remember <img src='http://nfriedly.com/techblog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><code>Prototype</code> to the win:</p>
<pre class="brush: jscript; title: ; notranslate">
function takesTwoParams(a, b){

	var args = Array.prototype.slice.call(arguments);

	alert(&quot; your parameters were &quot; + args.join(&quot;, &quot;));
	// yay, this works!

}
</pre>
<p>Let&#8217;s take a look at that a bit more in-depth:</p>
<p><code>Array</code>: This object is the original array that all other arrays inherit their properties from.</p>
<p><code>Array.prototype</code>:This gives us access to all the methods properties that each array inherits</p>
<p><code>Array.prototype.slice</code>: The original slice method that is given to all arrays via the prototype chain. We can&#8217;t call it directly though, because when it runs internally, it looks at the <code>this</code> keyword, and calling it here would make <code>this</code> point to <code>Array</code>, not our <code>arguments</code> variable.</p>
<p><code>Array.prototype.slice.call()</code>: <code>call()</code> and <code>apply()</code> are prototype methods of the <code>Function</code> object, meaning that they can be called on every function in javascript. These allow you to change what the <code>this</code> variable points to inside a given function.</p>
<p>And finally, you get a regular <code>array</code> back! This works because javascript returns a new object of type Array rather than whatever you gave it. This causes a lot of headaches for a <a href="http://dean.edwards.name/weblog/2006/11/hooray/">few</a> <a href="http://webreflection.blogspot.com/2008/03/sorry-dean-but-i-subclassed-array-again.html">people</a> who are trying to make subclasses of Array, but it&#8217;s very handy in our case!</p>
<h2><a name="gotchas"></a>Gotchas</h2>
<p>First, in Internet Explorer, DOM <code>NodeLists</code> are not considered to be javascript objects, so you cannot call <code>Array.prototype.slice</code> on them. If you want an array, you&#8217;ll have to loop through it the old fashioned way. Or use a hybrid function that tries it the fast way first, then the slow way if that doesn&#8217;t work. </p>
<pre class="brush: jscript; title: ; notranslate">
function hybridToArray(nodes){
	try{
		// works in every browser except IE
		var arr = Array.prototype.slice.call(nodes);
		return arr;
	} catch(err){
		// slower, but works in IE
		var arr = [],
		    length = nodes.length;
		for(var i=0; i &lt; length; i++){
			arr.push(nodes[i]);
		}
		return arr;
	}
}
</pre>
<p>See an example here: <a href="http://nfriedly.com/demos/ie-nodelist-to-array">http://nfriedly.com/demos/ie-nodelist-to-array</a>.</p>
<p>Second, arrays are objects, so you can do this, but it can get you some serious inconsistencies:</p>
<pre class="brush: jscript; title: ; notranslate">

arr = [];

arr[0] = &quot;first element&quot;; // adds item to the array

arr.length; // returns 1

arr.two = &quot;second element&quot;; // adds an item to the underlying object that array is built on top of.

arr.length; // still returns 1 !

// BUT...
for(i in arr){

	// this will hit both 0 and &quot;two&quot;

}
</pre>
<h3>A better solution: wrap arrays in an object if you need both worlds</h3>
<p>This is basically a less efficient method of the array subclassing links I mentioned above. While less efficient, it has the advantage of being simple and reliable.</p>
<p>That said, I wouldn&#8217;t recommend that you use this in most cases due to issues with speed and extra code requirements. It&#8217;s provided here as an example.</p>
<pre class="brush: jscript; title: ; notranslate">
// an example of a wrapper for an array.
// not recommended for most situations.

var ArrayContainer = function(arr){

	this.arr = arr || [];

	this.length = this.arr.length;

};

ArrayContainer.prototype.add=  function(item){

	index = this.arr.length;

	this.arr[index] = item;

	this.length = this.arr.length;

	return index;

};

ArrayContainer.prototype.get=  function(index){

	return this.arr[index];

};

ArrayContainer.prototype.forEach=  function(fn){

	if(this.arr.forEach) this.arr.forEach(fn);// use native code if it's there

	else {

		for(i in this.arr){

			fn( i, this.arr[i], this.arr );

		}
	}
};

var mySuperDooperArray = new ArrayContainer();
</pre>
<p>Now that your array is (somewhat) protected on the inside, you can loop through it&#8217;s items with <code>forEach()</code> and know that they will match it&#8217;s length. You can also add arbitrary properties to <code>ArrayContainer</code> or <code>mySuperDooperArray</code> and they <strong>won&#8217;t</strong> get pulled into your <code>forEach()</code> loop.</p>
<p>This example could be extended to completely protect the array if the need arose. </p>
<h2>An Even Better Solution: <a href="http://nfriedly.com/webdev">Hire a javascript expert</a>.</h2>
<p>nFriedly Web Development is a top ranked <a href="http://nfriedly.com/webdev/javascript">Javascript and AJAX ninja</a> with an extensive portfolio of proven results. I can bring your project to life and make it run faster than you ever imagined.  <a href="http://nfriedly.com/contact">Get in touch</a> with me or get a free <a href="http://nfriedly.com/estimate">instant estimate</a> for new projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

