<?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>PHP, MySQL, JavaScript, CSS tips and tricks</title>
	<atom:link href="http://blog.chochoy.com/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chochoy.com</link>
	<description>If you can think outside the box, you need a bigger box.</description>
	<lastBuildDate>Fri, 05 Aug 2011 12:10:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>JavaScript FizzBuzz variations</title>
		<link>http://blog.chochoy.com/archives/95?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-fizzbuzz-variations</link>
		<comments>http://blog.chochoy.com/archives/95#comments</comments>
		<pubDate>Fri, 05 Aug 2011 12:10:04 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Humour]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.chochoy.com/?p=95</guid>
		<description><![CDATA[FizzBuzz is a famous algorithmic problem used in software development job interviews: Display a list of numbers from 1 to 100, one per line. If the number is a multiple of 3, display Fizz instead. If the number is a &#8230; <a href="http://blog.chochoy.com/archives/95">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>FizzBuzz is a famous algorithmic problem used in software development job interviews:</p>
<ul>
<li>Display a list of numbers from 1 to 100, one per line.</li>
<li>If the number is a multiple of 3, display Fizz instead.</li>
<li>If the number is a multiple of 5, display Buzz instead.</li>
<li>If the number is a multiple of 3 and 5, display FizzBuzz instead.</li>
</ul>
<p>Here are 4 different JavaScript solutions to the FizzBuzz problem:</p>
<h2>The Junior Developer</h2>
<pre class="brush: jscript; title: ; notranslate">
for( i=1; i&lt;=100; i++) {
   if( (i % 3 == 0) &amp;&amp; (i % 5 == 0)) {
      document.write(&quot;FizzBuzz&quot;);
   } else if(i % 3 == 0) {
      document.write(&quot;Fizz&quot;);
   } else if(i % 5 == 0) {
      document.write(&quot;Buzz&quot;);
   } else {
      document.write(i);
   }
   document.write(&quot;&lt;br&gt;&quot;);
}
</pre>
<h2>The Experienced Developer</h2>
<pre class="brush: jscript; title: ; notranslate">
for( var i=1; i&lt;=100; i++) {
   if( i % 15 == 0) {
      str += &quot;FizzBuzz&quot;;
   } else if(i % 3 == 0) {
      str += &quot;Fizz&quot;;
   } else if (i % 5 == 0) {
      str += &quot;Buzz&quot;;
   } else {
      str += i;
   }

   str += &quot;&lt;br&gt;&quot;;
}
document.write(str);
</pre>
<h2>The JavaScript Guru</h2>
<pre class="brush: jscript; title: ; notranslate">
// Super-optimised version of FizzBuzz
var array = [];
array[3] = array[6] = array[9] = array[12] = &quot;Fizz&lt;br&gt;&quot;;
array[5] = array[10] = &quot;Buzz&lt;br&gt;&quot;;
array[0] = &quot;FizzBuzz&lt;br&gt;&quot;;
var str = &quot;&quot;;

for( var i=1; i&lt;=100; ++i) {
   str += array[ i%15 ] || i + &quot;&lt;br&gt;&quot;;
}

window.onload = function() {
   document.body.innerHTML = str;
}
</pre>
<h2>The Senior Developer</h2>
<pre class="brush: jscript; title: ; notranslate">
// TODO Fix missing 'Buzz' at the end.
// TODO Rewrite using XML
try {
   document.write(&quot;1&lt;br&gt; 2&lt;br&gt; Fizz&lt;br&gt; 4&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 7&lt;br&gt; 8&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 11&lt;br&gt; Fizz&lt;br&gt; 13&lt;br&gt; 14&lt;br&gt; FizzBuzz&lt;br&gt; 16&lt;br&gt; 17&lt;br&gt; Fizz&lt;br&gt; 19&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 22&lt;br&gt; 23&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 26&lt;br&gt; Fizz&lt;br&gt; 28&lt;br&gt; 29&lt;br&gt; FizzBuzz&lt;br&gt; 31&lt;br&gt; 32&lt;br&gt; Fizz&lt;br&gt; 34&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 37&lt;br&gt; 38&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 41&lt;br&gt; Fizz&lt;br&gt; 43&lt;br&gt; 44&lt;br&gt; FizzBuzz&lt;br&gt; 46&lt;br&gt; 47&lt;br&gt; Fizz&lt;br&gt; 49&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 52&lt;br&gt; 53&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 56&lt;br&gt; Fizz&lt;br&gt; 58&lt;br&gt; 59&lt;br&gt; FizzBuzz&lt;br&gt; 61&lt;br&gt; 62&lt;br&gt; Fizz&lt;br&gt; 64&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 67&lt;br&gt; 68&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 71&lt;br&gt; Fizz&lt;br&gt; 73&lt;br&gt; 74&lt;br&gt; FizzBuzz&lt;br&gt; 76&lt;br&gt; 77&lt;br&gt; Fizz&lt;br&gt; 79&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 82&lt;br&gt; 83&lt;br&gt; Fizz&lt;br&gt; Buzz&lt;br&gt; 86&lt;br&gt; Fizz&lt;br&gt; 88&lt;br&gt; 89&lt;br&gt; FizzBuzz&lt;br&gt; 91&lt;br&gt; 92&lt;br&gt; Fizz&lt;br&gt; 94&lt;br&gt; Buzz&lt;br&gt; Fizz&lt;br&gt; 97&lt;br&gt; 98&lt;br&gt; Fizz&lt;br&gt;&quot;);
} catch(exception) {
   document.write(&quot;Error: browser doesn't support the document.write() method.&quot;);
}
</pre>
<h2>Bonus &#8211; The CSS Guru</h2>
<pre class="brush: jscript; html-script: true; title: ; notranslate">
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
body {
   counter-reset: number;
}

a:after {
   content: counter(number);
   counter-increment: number;
   display: block;
}

a:nth-child(3n):after, a:nth-child(15n):before {
   content: &quot;Fizz&quot;;
}

a:nth-child(5n):after {
   content: &quot;Buzz&quot;;
}
&lt;/style&gt;
&lt;script&gt;
window.onload = function() {
   for(var i=0; i&lt;100; ++i) {
      document.body.appendChild(document.createElement(&quot;a&quot;));
   }
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 1632px; width: 1px; height: 1px; overflow: hidden;">
<pre>FizzBuzz Ver 2.1</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.chochoy.com/archives/95/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove Internet Explorer&#8217;s &#8220;Compatibility View&#8221; button</title>
		<link>http://blog.chochoy.com/archives/69?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=remove-internet-explorer-compatibility-view</link>
		<comments>http://blog.chochoy.com/archives/69#comments</comments>
		<pubDate>Tue, 05 Jul 2011 11:14:50 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.chochoy.com/?p=69</guid>
		<description><![CDATA[Compatibility View? With Internet Explorer 8, Microsoft introduced the concept of Compatibility View. If a page does not display properly using the normal standard mode, IE8 can detect it, and automatically switch to compatibility mode. There is also a button &#8230; <a href="http://blog.chochoy.com/archives/69">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Compatibility View?</h2>
<p>With Internet Explorer 8, Microsoft introduced the concept of Compatibility View.</p>
<p>If a page does not display properly using the normal standard mode, IE8 can detect it, and automatically switch to compatibility mode.</p>
<p>There is also a button to switch modes manually: It&#8217;s a button that looks like a torn up piece of paper, and you will find it in the navigation bar.</p>
<p><img title="Compatibility View button in IE8" src="http://blog.chochoy.com/wp-content/uploads/compatibility-view-button-internet-explorer.png" alt="Compatibility View button in IE" width="960" height="90" /></p>
<p>Did you know that there was a way to prevent this button from showing on your website?</p>
<h2>The X-UA-Compatible header</h2>
<p>Internet Explorer 8 added support for a new header called X-UA-Compatible.</p>
<p>This header tells Internet Explorer which engine version should be used to display a page.</p>
<p>If its value is set to IE=egde, this  forces Internet Explorer to always use the &#8220;standard&#8221; mode. When you do this, IE8, IE9 (and possibly future versions of IE) will not display the &#8220;Compatibility View&#8221; button.</p>
<h2>Implementation</h2>
<h3>In a meta tag</h3>
<p>Inside your document &lt;head&gt;, insert the following meta tag:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
</pre>
<h3>In PHP</h3>
<pre class="brush: php; title: ; notranslate">
header('X-UA-Compatible: IE=edge');
</pre>
<h3>In .htaccess</h3>
<pre class="brush: plain; title: ; notranslate">
&lt;FilesMatch &quot;\.(php|html)$&quot;&gt;
   Header set X-UA-Compatible &quot;IE=edge&quot;
&lt;/FilesMatch&gt;
</pre>
<p>More information about the X-UA-Compatible header and its possibilities is available at the <a href="http://msdn.microsoft.com/en-us/library/cc288325%28v=vs.85%29.aspx">Microsoft MSDN Library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chochoy.com/archives/69/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript &#8211; Test if a library failed to load</title>
		<link>http://blog.chochoy.com/archives/48?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-test-if-library-failed-to-load</link>
		<comments>http://blog.chochoy.com/archives/48#comments</comments>
		<pubDate>Thu, 23 Jun 2011 17:00:31 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.chochoy.com/?p=48</guid>
		<description><![CDATA[A small simple JavaScript block to detect and alert you if an external library fails to load:]]></description>
			<content:encoded><![CDATA[<p>A small simple JavaScript block to detect and alert you if an external library fails to load:</p>
<pre class="brush: jscript; title: ; notranslate">
try {
   switch( &quot;undefined&quot; ) {
      case typeof $: throw &quot;jQuery&quot;;
      case typeof $.ui: throw &quot;jQuery UI&quot;;
      case typeof TinyMCE: throw &quot;tinyMCE&quot;;
      ...
      case typeof yourFavouriteLibrary: throw &quot;Your favourite library&quot;;
   }
} catch( missingLibrary ) {
   alert(&quot;Error: failed to load &quot; + missingLibrary);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.chochoy.com/archives/48/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load Google Analytics script with jQuery</title>
		<link>http://blog.chochoy.com/archives/51?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=load-google-analytics-script-with-jquery</link>
		<comments>http://blog.chochoy.com/archives/51#comments</comments>
		<pubDate>Tue, 14 Jun 2011 10:12:22 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.chochoy.com/?p=51</guid>
		<description><![CDATA[Idea If you have ever set up Google Analytics for your website, you are probably familiar with this piece of code: If you use jQuery on your site, you can reduce the amount of code required to load the Google &#8230; <a href="http://blog.chochoy.com/archives/51">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Idea</h2>
<p>If you have ever set up Google Analytics for your website, you are probably familiar with this piece of code:</p>
<pre class="brush: jscript; highlight: [5,6,7,8,9]; title: ; notranslate">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '## user account ##']);
_gaq.push(['_trackPageview']);

(function() {
   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</pre>
<p>If you use jQuery on your site, you can reduce the amount of code required to load the Google Analytics script:</p>
<pre class="brush: jscript; highlight: [5]; title: ; wrap-lines: true; notranslate">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '## user account ##']);
_gaq.push(['_trackPageview']);

$.getScript(&quot;https:&quot; == document.location.protocol ? &quot;https://ssl&quot; : &quot;http://www&quot;) + &quot;.google-analytics.com/ga.js&quot;;
</pre>
<h2>Further reductions</h2>
<p>&#8230;and if you are using only one Google Analytics account, and do not have SSL installed on your site, you may reduce the size even further:</p>
<pre class="brush: jscript; title: ; wrap-lines: true; notranslate">
var _gaq = [[&quot;_setAccount&quot;, &quot;## user account ##&quot;], [&quot;_trackPageview&quot;]];
$.getScript(&quot;http://www.google-analytics.com/ga.js&quot;);
</pre>
<h2>Drawback</h2>
<p>jQuery <strong>getScript()</strong> will not work on older browsers (Internet Explorer 5.X, Firefox 1.X, and previous versions). This means these browsers will not show in your Google Analytics reports.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chochoy.com/archives/51/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simulating PHP $_GET in JavaScript (with multiple dimensions)</title>
		<link>http://blog.chochoy.com/archives/5?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simulating-php-_get-in-javascript</link>
		<comments>http://blog.chochoy.com/archives/5#comments</comments>
		<pubDate>Mon, 06 Jun 2011 17:38:05 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.chochoy.com/?p=5</guid>
		<description><![CDATA[Idea JavaScript does not provide a built-in way to access the variables passed into the URL of a page (the query string). However it is possible to use the standard window.location object to retrieve these variables and their associated values. &#8230; <a href="http://blog.chochoy.com/archives/5">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Idea</h2>
<p>JavaScript does not provide a built-in way to access the variables passed into the URL of a page (the <em>query string</em>).</p>
<p>However it is possible to use the standard <a title="W3 Schools" href="http://www.w3schools.com/jsref/obj_location.asp" target="_blank">window.location object</a> to retrieve these variables and their associated values.</p>
<p><strong>window.location.search</strong> contains the query string (and the additional <em>&#8216;?&#8217;</em> at the start). We can process it and store our results into a multidimensional array, as the following scripts demonstrate:</p>
<h2>The script</h2>
<pre class="brush: jscript; title: ; notranslate">
var $_GET = (function() {
// Split the params part of the URL into &quot;key=value&quot; pairs
   var pairs = window.location.search.slice(1).split(&quot;&amp;&quot;);
   var params = [];

// Split each pair and store their values into the params array
   for(i in pairs) {
      var splitPairs = pairs[i].split(&quot;=&quot;);
      var name = unescape(splitPairs[0]);
      var value = unescape(splitPairs[1]);

// Check for valid name
      var match = name.match(/^(\w+)(?:\[\w*\])*$/);
      if( !match ) continue;

      var dimensions = [match[1]];
      var sub = match[0];
      var re = /\[(\w*)\]/g;

      while( match = re.exec(sub) ) {
         var dim = match[1];

         if(dim == &quot;&quot;) {
            dim = null;
         } else if( dim == parseInt(dim)) {
            dim = parseInt(dim);
         }

         dimensions.push(dim);
      }

// Build params array with multiple dimensions
      (function(p, d) {

         var s = d.shift()

         if( s === null ) {
            s = -1;

            for(var i in p) {
               if(i &gt; s) {
                   s = i;
               }
            }
            ++s;
         }

         if( d.length ) {
            p[s] = p[s] || [];
            arguments.callee(p[s], d);
         } else {
            p[s] = value;
         }
      })(params, dimensions);
   }
   return params;
})();
</pre>
<p>This script creates a <strong>$_GET</strong> array containing variables passed to the page via URL parameters, like in PHP.<br />
It will also work when multi-dimensional variables appear.</p>
<h2>Why so complicated?</h2>
<p>This script is can deal with the case of multi-dimensional arrays with or without keys.<br />
It also reproduces the behaviour of <a title="The array type in PHP" href="http://www.php.net/manual/en/language.types.array.php">the array type</a> in PHP.</p>
<blockquote><p>If a key is not specified for a value, the maximum of the integer indices is taken and the new key will be that value     plus 1. If a key that already has an assigned value is specified, that value     will be overwritten.</p></blockquote>
<p>This means if we use the following URL:</p>
<p>http://www.example.com?person[3]=John&#038;person[]=Peter&#038;person[]=Jack</p>
<p>The $_GET variable will be:</p>
<table width="100%">
<tbody>
<tr>
<th>PHP</th>
<th>JavaScript</th>
</tr>
<tr>
<td>
<pre class="brush: plain; gutter: false; title: ; notranslate">
Array
(
   [person] =&gt;; Array
   (
      [3] =&gt; John
      [4] =&gt; Peter
      [5] =&gt; Jack
   )
)
</pre>
</td>
<td>
<pre class="brush: plain; gutter: false; title: ; notranslate">
[
   person: [
      undefined,
      undefined,
      undefined,
      &quot;John&quot;,
      &quot;Peter&quot;,
      &quot;Jack&quot;
   ]
]
</pre>
</td>
</tr>
</tbody>
</table>
<h2>Usage example</h2>
<pre class="brush: jscript; title: ; notranslate">
// If the page URL is www.example.com/page.html?user=John%20Smith
// this will display &quot;Hello John Smith&quot;
alert(&quot;Hello &quot; + $_GET[&quot;user&quot;]);

// If the URL is www.example.com/page.html?user[name]=John&amp;user[surname]=Smith
alert(&quot;Hello &quot; + $_GET[&quot;user&quot;][&quot;name&quot;] + &quot; &quot; + $_GET[&quot;user&quot;][&quot;surname&quot;]);

// If the URL is www.example.com/page.html?user[]=John&amp;user[]=Peter
alert($_GET[&quot;user&quot;][0]); // John
alert($_GET[&quot;user&quot;][1]); // Peter
</pre>
<h2>XSS injections?</h2>
<p>Inevitably&#8230; If users can change parameters that affect the content of a page, this opens up the danger of XSS injections.</p>
<pre class="brush: jscript; title: ; notranslate">
// www.example.com/page.html?name=John&lt;script src=&quot;maliciousscript.js&quot;&gt;&lt;/script&gt;

if($_GET[&quot;name&quot;] == &quot;John&quot;) {
   doSomething(); // OK
}

document.write($_GET[&quot;name&quot;]); // Ouch!
</pre>
<p>Principles that apply for server-side scripting also apply in this case: Do not display anything that comes directly from the query string. Always sanitise first.</p>
<p>JavaScript does not provide a function to strip HTML tags or replace special characters with their equivalent HTML code, so you will have to write your own if you need to display variables from the query string.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chochoy.com/archives/5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

