<?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>Geekality &#187; Javascript</title>
	<atom:link href="http://www.geekality.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:53:50 +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>JavaScript: Delayed keypress event (sort of)</title>
		<link>http://www.geekality.net/2012/02/05/javascript-delayed-keypress-event-sort-of/</link>
		<comments>http://www.geekality.net/2012/02/05/javascript-delayed-keypress-event-sort-of/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 17:48:18 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1935</guid>
		<description><![CDATA[Wanted to do an AJAX call whenever the content of a field was changed. This can be done simply with the onchange event, but the problem is that you have to tab out of the field to make it fire. &#8230; <a href="http://www.geekality.net/2012/02/05/javascript-delayed-keypress-event-sort-of/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wanted to do an AJAX call whenever the content of a field was changed. This can be done simply with the onchange event, but the problem is that you have to tab out of the field to make it fire. You can also do it with the keypress event, but then you&#8217;ll get one AJAX call for each and every keypress, which is just silly.</p>
<p>Found a <a href="http://www.openjs.com/scripts/events/check_after_typing.php">nice and easy solution</a>, and here it is slightly changed.</p>
<p><span id="more-1935"></span></p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> key_press_counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> check<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Do something</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> key_press_counter<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Check field, do an ajax call, whatever</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; check<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">keypress</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setTimeout<span style="color: #009900;">&#40;</span>check<span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #339933;">++</span>key_press_counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>It&#8217;s a bit sneaky, but you should be able to deduce how it works  <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':wink:' class='wp-smiley smiley-20' /> Working sample over at <a href="http://samples.geekality.net/delay-check">samples.geekality.net/delay-check</a>.</p>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1935&amp;md5=b81f378ebe7eed24928013c3adc75d0e" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2012/02/05/javascript-delayed-keypress-event-sort-of/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Measure upload time and speed with PHP and Javascript</title>
		<link>http://www.geekality.net/2011/11/20/measure-upload-time-and-speed-with-php-and-javascript/</link>
		<comments>http://www.geekality.net/2011/11/20/measure-upload-time-and-speed-with-php-and-javascript/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 07:34:42 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Timer]]></category>
		<category><![CDATA[Upload]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1876</guid>
		<description><![CDATA[Stumbled upon a question on StackOverflow the other day which got me curious. The question was about how to measure how long it takes to upload a file to a PHP script. This is what had been tried out: $upload_time &#8230; <a href="http://www.geekality.net/2011/11/20/measure-upload-time-and-speed-with-php-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Stumbled upon <a href="http://stackoverflow.com/q/8181291/39321">a question on StackOverflow</a> the other day which got me curious. The question was about how to measure how long it takes to upload a file to a PHP script. This is what had been tried out:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$upload_time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_TIME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></div>
<p>This pretty much always returns zero, even though the uploading actually took many seconds, because the request start time is <em>after</em> the server has received the post data. That we actually just get how long the script took to run, which of course is pretty close to zero seconds. So, what can we do?</p>
<ol>
<li>Right before data is posted, nudge the server with an AJAX call which stores the current timestamp in a session variable
<li>Post the data
<li>Compare current timestamp with the one stored in step 1
</ol>
<p>Wasn&#8217;t sure how it would work, but seems to work pretty well. There will of course be a very tiny difference since the AJAX request will be a bit part of the time, but compared to the upload time it shouldn&#8217;t matter much. Anyways, here&#8217;s how you could do it <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p><span id="more-1876"></span></p>
<p>In your HTML you need your form, obviously.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Upload&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></div></div>
<p>And then we need the Javascript which does the AJAX request (using <a href="http://api.jquery.com/jQuery.ajax/">jQuery</a>). Notice that this makes sure we have gotten a response from the timer script before we actually submit the form.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'start-timer.php'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>The start timer simply stores a timestamp in a session variable.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then in the upload script we simply need to compare that time with the current timestamp to figure out how long this took.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$upload_time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// You should of course probably also check if the upload was OK and all that ;)</span></div></div>
<p>And that&#8217;s pretty much all there is to it! Working sample at <a href="http://samples.geekality.net/upload-timer">samples.geekality.net/upload-timer</a>. </p>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1876&amp;md5=f6f20c5127f2d1d601c665e890832113" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2011/11/20/measure-upload-time-and-speed-with-php-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: What&#8217;s a valid JavaScript identifier (or function name)?</title>
		<link>http://www.geekality.net/2011/08/03/valid-javascript-identifier/</link>
		<comments>http://www.geekality.net/2011/08/03/valid-javascript-identifier/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 15:09:49 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Language Spec]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expression]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1739</guid>
		<description><![CDATA[After another reply to a question I&#8217;ve had on StackOverflow for a while, I decided that I perhaps should add another level of security to my method of providing JSONP from PHP. The way I did it before, I didn&#8217;t &#8230; <a href="http://www.geekality.net/2011/08/03/valid-javascript-identifier/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After another reply to <a href="http://stackoverflow.com/a/6892827/39321" title="PHP: Is this safe for providing JSONP?" class="broken_link">a question</a> I&#8217;ve had on StackOverflow for a while, I decided that I perhaps should add another level of security to my method of <a href="/?p=1021" title="How to easily provide JSON and JSONP">providing JSONP</a> from PHP. The way I did it before, I didn&#8217;t do any checking on the provided callback. This means that someone could technically put whatever they wanted in there, including malicious code. So, therefore it might be a good idea to check if the callback, which should be a function name, actually <em>is</em> a valid function name. But,</p>
<p><span id="more-1739"></span></p>
<h2>What is valid?</h2>
<p>To figure that out, we need a look in the <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" title="ECMA-262, 5.1 Edition / June 2011">ECMAScript Language Specification</a>. In chapter 13 on functions, we find that a function name is a so-called identifier, which is described in chapter 7.6. There we can find the following facts:</p>
<table style="font-size: 80%">
<tr>
<th>Identifier</th>
<td>&lt;IdentifierName&gt; <strong>but not</strong> &lt;ReservedWord&gt;</td>
</tr>
<tr>
<th>IdentifierName</th>
<td>&lt;IdentifierStart&gt;<br />
&lt;IdentifierName&gt; &lt;IdentifierPart&gt;
</td>
</tr>
<tr>
<th>IdentifierStart</th>
<td>&lt;UnicodeLetter&gt;<br />
$<br />
_<br />
\ &lt;UnicodeEscapeSequence&gt;</td>
</tr>
<tr>
<th>IdentifierPart</th>
<td>&lt;IdentifierStart&gt;<br />
&lt;UnicodeCombiningMark&gt;<br />
&lt;UnicodeDigit&gt;<br />
&lt;UnicodeConnectorPunctuation&gt;<br />
&lt;ZWNJ&gt;<br />
&lt;ZWJ&gt;</td>
</tr>
<tr>
<th>UnicodeLetter</th>
<td>Uppercase letter (Lu)<br />
Lowercase letter (Ll)<br />
Titlecase letter (Lt)<br />
Modifier letter (Lm)<br />
Other letter (Lo)<br />
Letter number (Nl)</td>
</tr>
<tr>
<th>UnicodeCombiningMark</th>
<td>Non-spacing mark (Mn)<br />
Combining spacing mark (Mc)</td>
</tr>
<tr>
<th>UnicodeDigit</th>
<td>Decimal number (Nd)</td>
</tr>
<tr>
<th>UnicodeConnectorPunctuation</th>
<td>Connector punctuation (Pc)</td>
</tr>
<tr>
<th>UnicodeEscapeSequence</th>
<td>The definitions of the nonterminal UnicodeEscapeSequence is given in 7.8.4</td>
</tr>
<tr>
<th>ZWNJ</th>
<td>U+200C (<a href="http://en.wikipedia.org/wiki/ZWNJ">Zero-width non-joiner</a> <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=')' class='wp-smiley smiley-20' /> </td>
</tr>
<tr>
<th>ZWJ</th>
<td>U+200D (<a href="http://en.wikipedia.org/wiki/ZWJ">Zero-width joiner</a> <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=')' class='wp-smiley smiley-20' /> </td>
</tr>
<tr>
<th>ReservedWord</th>
<td>&lt;Keyword&gt;<br />
&lt;FutureReservedWord&gt;<br />
&lt;NullLiteral&gt;<br />
&lt;BooleanLiteral&gt;</td>
</tr>
<tr>
<th>Keyword</th>
<td>break, do, instanceof, typeof, case, else, new, var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try</td>
</tr>
<tr>
<th>FutureReservedWord</th>
<td>class, enum, extends, super, const, export, import<br />
    implements, let, private, public, yield, interface, package, protected, static</td>
</tr>
<tr>
<th>NullLiteral</th>
<td>null</td>
</tr>
<tr>
<th>BooleanLiteral</th>
<td>true, false</td>
</tr>
</table>
<p>Looks long, but not too complicated.</p>
<h2>Checking if a string is valid</h2>
<p>To check if a string is a valid identifier is now pretty easy. We just need to make sure the string matches the allowed syntax, and that it&#8217;s not a reserved word. The first we can solve with a regular expression and the second with a simple white list array. For example, something along the following lines.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> is_valid_identifier<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$identifier_syntax</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$reserved_words</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'break'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'do'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'instanceof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'typeof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'case'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'else'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'new'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'var'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'catch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'finally'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'void'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'continue'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'for'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'switch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'while'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'debugger'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'function'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'with'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'default'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'if'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'throw'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'in'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'try'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'enum'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'extends'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'super'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'const'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'export'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'import'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'implements'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'let'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'private'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'public'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yield'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'interface'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'package'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'protected'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'static'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'false'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier_syntax</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$reserved_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Not too complex, although the regular expression might look a bit nuts at first because of all the Unicode character groups. You might find regular expressions other places to do this that uses a-z for the letters, but as you saw from the specification that won&#8217;t cover much of what&#8217;s actually valid. </p>
<p>I built the expression using the very helpful <a href="http://www.regexbuddy.com/">RegexBuddy</a> and exported an HTML explanation of it. Also threw together a tiny identifier validator thing where you can test it out with. You find it all at <a href="http://samples.geekality.net/js-identifiers">samples.geekality.net/js-identifiers</a>.</p>
<p>And that&#8217;s that. Hope that might be helpful for someone and please let me know if you find any issues with it!</p>
<blockquote><p><strong>Note:</strong> I have ignored the issue with the Unicode escape sequences for now as I&#8217;m not quite sure how to best handle those. From the specification:</p>
<blockquote><p>A UnicodeEscapeSequence cannot be used to put a character into an IdentifierName that would otherwise be illegal. In other words, if a \UnicodeEscapeSequence sequence were replaced by its UnicodeEscapeSequence&#8217;s <abbr title="Character Value">CV</abbr>, the result must still be a valid IdentifierName that has the exact same sequence of characters as the original IdentifierName. All interpretations of identifiers within this specification are based upon their actual characters regardless of whether or not an escape sequence was used to contribute any particular characters.</p></blockquote>
<p>So, I&#8217;m not sure if there is a way to just convert those sequences into actual characters or if this is done automatically by PHP as they come in as GET parameters or what. Either way, my code above there is ignoring them. This means, identifiers with escape sequences will not be considered valid. If you have some good ideas on how to handle it, please leave a comment <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p></blockquote>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1739&amp;md5=d6a9a0cd633273d4fde200b37d4ebbea" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2011/08/03/valid-javascript-identifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery: Nice and smooth hover effect</title>
		<link>http://www.geekality.net/2010/08/26/jquery-nice-and-smooth-hover-effect/</link>
		<comments>http://www.geekality.net/2010/08/26/jquery-nice-and-smooth-hover-effect/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 17:44:31 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery UI]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1165</guid>
		<description><![CDATA[Needed to add a hover effect on some table rows and wanted to make it look nice. Think I managed to get it quite smooth in the end and thought I could share it. 12345678910111213141516171819202122232425262728293031// In the document ready event &#8230; <a href="http://www.geekality.net/2010/08/26/jquery-nice-and-smooth-hover-effect/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Needed to add a hover effect on some table rows and wanted to make it look nice. Think I managed to get it quite smooth in the end and thought I could share it.</p>
<p><span id="more-1165"></span></p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// In the document ready event</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Attach handler</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table tbody'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tr'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'hover'</span><span style="color: #339933;">,</span> hoverHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> hoverHandler<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// When mouse comes over</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Stop animation where it is</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Start fading up</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'#fd8'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// When mouse goes out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Jump to end of animation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #000066;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Start fading down</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>backgroundColor<span style="color: #339933;">:</span><span style="color: #3366CC;">'transparent'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You can check out a live version at <a href="http://samples.geekality.net/hover-effect/">samples.geekality.net/hover-effect</a></p>
<p>I would like to have used a class instead of a hard-coded color though. I tried to use addClass and removeClass, but with animation those proved to be quite buggy in this case. The problem with hard-coded colors is first of all that it generally is much better to collect styles in style-sheets than to have them spread out in other code. Secondly, doing it this way will override any background-color the rows might have had before you hovered over them.</p>
<p>A case where the second can ruin things a bit is if you have alternated background colors. This could be worked around if you take it into account in the handler though. Just for fun I tried to do just that: <a href="http://samples.geekality.net/hover-effect/alt.php">samples.geekality.net/hover-effect/alt.php</a></p>
<p>Note that this might not work so well if your table is dynamic (if you sort, add or remove rows for example). But anyways, looks pretty cool I think <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' class='wp-smiley smiley-13' /> </p>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1165&amp;md5=7e514390e40efb488eda31f3a63d181b" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/08/26/jquery-nice-and-smooth-hover-effect/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript: Uppercase first letter in a string</title>
		<link>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/</link>
		<comments>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 13:21:07 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1052</guid>
		<description><![CDATA[In PHP there is a very handy function called ucfirst which Returns a string with the first character of str capitalized, if that character is alphabetic. Needed that in JavaScript, but discovered there was no such thing. Luckily I quickly &#8230; <a href="http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In PHP there is a very handy function called <a href="http://php.net/ucfirst">ucfirst</a> which </p>
<blockquote><p>Returns a string with the first character of str capitalized, if that character is alphabetic.</p></blockquote>
<p>Needed that in JavaScript, but discovered there was no such thing.</p>
<p><span id="more-1052"></span></p>
<p>Luckily I quickly found <a href="http://stackoverflow.com/q/1026069/39321#1026087">a function</a> over at StackOverflow that I adjusted slightly and added to the string class:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">ucfirst</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Can be used like so:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'some text'</span>.<span style="color: #660066;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Alerts: Some text</span></div></div>
<h2>Alternative CSS method</h2>
<p>In many cases it might be good to just handle this with CSS instead. For example, to uppercase the first letter in all list items, you can do this:</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">li<span style="color: #3333ff;">:first-letter<br />
</span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">text-transform</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">uppercase</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></div>
<p>Weeee <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='^_^' class='wp-smiley smiley-9' /> Ok, back to work&#8230;</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: How to easily provide JSON and JSONP</title>
		<link>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/</link>
		<comments>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 18:03:28 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[CORS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSONP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1021</guid>
		<description><![CDATA[Would you like to grab some server-side data through an AJAX call? For example by using the handy jQuery.ajax method? A good data format to use then is JavaScript Object Notation, more commonly known as JSON. Providing data in the &#8230; <a href="http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Would you like to grab some server-side data through an AJAX call? For example by using the handy <a href="http://api.jquery.com/jQuery.ajax/">jQuery.ajax</a> method?</p>
<p>A good data format to use then is <a href="http://en.wikipedia.org/wiki/JSON">JavaScript Object Notation</a>, more commonly known as JSON. Providing data in the JSON format with PHP is super duper simple  <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' class='wp-smiley smiley-3' /> </p>
<p><span id="more-1021"></span></p>
<h2>JSON</h2>
<p>All you need to do on the server side is to set the <code class="codecolorer text default"><span class="text">content-type</span></code> to <code class="codecolorer text default"><span class="text">application/json</span></code>, encode your data using the <a href="http://php.net/json_encode">json_encode</a> function and output it.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can test it out for example in the <a href="http://getfirebug.com/">FireBug</a> console by running this line:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: [1,2,3,4,5,6,7,8,9]</span></div></div>
<p>You should see the request being done in the console and also in the Net tab. If it didn&#8217;t work, you might be subject to the following:</p>
<blockquote><p>Due to browser security restrictions, most &#8220;Ajax&#8221; requests are subject to the <a title="Same Origin Policy on Wikipedia" href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>; the request can not successfully retrieve data from a different domain, subdomain, or protocol.</p></blockquote>
<p>A nice and simple solution to that problem is JSON with padding, also known as JSONP.</p>
<h2>JSONP</h2>
<p>As stated on <a href="http://en.wikipedia.org/wiki/JSON#JSONP">Wikipedia</a>,</p>
<blockquote><p>JSONP or &#8220;JSON with padding&#8221; is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server.</p></blockquote>
<p>I&#8217;m not sure I get everything about JSONP, but I have used it and I know that  it works <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' class='wp-smiley smiley-13' /> It&#8217;s almost just as easy as plain JSON actually, and all that&#8217;s needed is to wrap the JSON encoded data in a callback function provided as a GET parameter.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span></div></div>
<p>You can test it by running</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #339933;">,</span> dataType<span style="color: #339933;">:</span><span style="color: #3366CC;">'jsonp'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: jsonp1277656587731([1,2,3,4,5,6,7,8,9])</span></div></div>
<p>This time you won&#8217;t see the call in the FireBug console though, but only in the Net tab. That&#8217;s because the data is loaded in a script tag instead of through an actual AJAX call. Thankfully jQuery handles all of that for you behind the scenes, so you don&#8217;t have to worry about it at all. You&#8217;ll get the same JSON data in your response handler, ready to be used however you want to <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p>What if you&#8217;d like to provide both formats?</p>
<h2>JSON and JSONP together</h2>
<p>Easy as cake actually. You just need to check if the callback parameter is set or not:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; ? <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_GET['callback']}</span>(<span style="color: #006699; font-weight: bold;">$json</span>)&quot;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">:</span> <span style="color: #000088;">$json</span><span style="color: #339933;">;</span></div></div>
<p>And that&#8217;s all! Although there are some additions you could do if you want:</p>
<h2>Cross-Origin Resource Sharing (CORS)</h2>
<p>When testing this you might have noticed it will not work cross-domain. This is because of security stuff in your browser. So, if you want everyone to be able to get access to some JSON data of yours, you need to flag that this is OK. One way to do this, is through CORS:</p>
<blockquote><p>Cross-Origin Resource Sharing (CORS) is a specification that enables a truly open access across domain-boundaries.<br />
&#8230;<br />
CORS defines how browsers and servers communicate when accessing sources across origins using HTTP headers to allow both the browser and the server to know enough about each other to determine if the request or response should succeed or fail. &#8212; <a href="http://enable-cors.org">enable-cors.org</a></p></blockquote>
<p>You can read more about it on the <a href="http://enable-cors.org">enable-cors.org</a> website, but all you should need to do if you want your JSON data accessible from everywhere is to add a single header.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;access-control-allow-origin: *&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Rest of script...</span></div></div>
<p>It should now be accessible from everywhere <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<h2>Added security</h2>
<p>Since the sender can send whatever they want as a callback, you could potentially have someone injecting malicious code there. I&#8217;m not sure how likely this is to happen, but it is a possibility. Could for example be used to steal cookies. Not the baked kind&#8230; So, although it does make things a bit uglier, it&#8217;s better to be safe than sorry I suppose? So I did <a href="/?p=1739">some research</a> and ended up with a function I could use to check that the callback is indeed a valid JavaScript identifier. Here&#8217;s the code with the added security.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> is_valid_callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$identifier_syntax</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$reserved_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'break'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'do'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'instanceof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'typeof'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'case'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'else'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'new'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'var'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'catch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'finally'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'void'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'continue'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'for'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'switch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'while'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'debugger'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'function'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'with'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'default'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'if'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'throw'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delete'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'in'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'try'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'enum'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'extends'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'super'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'const'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'export'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'import'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'implements'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'let'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'private'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'public'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yield'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'interface'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'package'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'protected'</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'static'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'null'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'false'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier_syntax</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$reserved_words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># JSON if no callback<br />
</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># JSONP if valid callback<br />
</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_valid_callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_GET['callback']}</span>(<span style="color: #006699; font-weight: bold;">$json</span>)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Otherwise, bad request<br />
</span><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status: 400 Bad Request'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Please comment if you have any feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p><ins datetime="2010-12-11T06:28:34+00:00"><strong>Update:</strong> Added charset to the content-type header as recommended in <a href="http://stackoverflow.com/q/3128062/39321#3128948">an answer</a> to a question I posted about this on StackOverflow.</ins></p>
<p><ins datetime="2011-08-03T15:14:15+00:00"><strong>Update:</strong> Added stuff about callback checking. Recommended in new answers to <a href="http://stackoverflow.com/q/3128062/39321">my question at StackOverflow</a>.</ins></p>
<p><ins datetime="2011-10-31T18:57:00+00:00"><strong>Update:</strong> Discovered an easy way to compress the JSON data! <a href="/?p=1847">Blogged about it</a> too <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </ins></p>
<p><ins datetime="2012-01-08T13:23:18+00:00"><strong>Update:</strong> Added some info about <a href="http://enable-cors.org">cross-origin resource sharing</a></ins></p>
<p class="wp-flattr-button"></p> <p><a href="http://www.geekality.net/?flattrss_redirect&amp;id=1021&amp;md5=c00077187379876f01ba7e253aab6dde" title="Flattr" target="_blank"><img src="http://www.geekality.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

