Queueing jQuery scripts before the library

When trying to queue jQuery scripts to run before loading the library itself, this usually results in that code not working. If you want to load jQuery at the end of your page (or asynchronously) but you may need to include jQuery within a template part way through the page the following code allows you attach and queue up scripts to run when jQuery initialises.

if ( ! window.deferAfterjQueryLoaded ) {
    window.deferAfterjQueryLoaded = [];
    Object.defineProperty(window, "$", {
        set: function(value) {
            window.setTimeout(function() {
                $.each(window.deferAfterjQueryLoaded, function(index, fn) {
            }, 0);
            Object.defineProperty(window, "$", { value: value });

        configurable: true

window.deferAfterjQueryLoaded.push(function() {
    //... some code that needs to be run

What this does is:

  1. Defines deferAfterjQueryLoaded  lazily, so you don't need to inject that into head .
  2. Defines a setter for window.$ . When jQuery loads, one of the last things it does is assign to the global $ variable. This allows you to trigger a function when that happens.
  3. Schedules the deferred functions to run as soon as possible after the jQuery script finishes (setTimeout(..., 0);) .
  4. Has the setter remove itself.

For complete cleanliness you could have the scheduled function remove deferAfterjQueryLoaded  as well.


This answer is provided from StackOverflow.

GET parameters with JavaScript

function getSearchParameters() {
      var prmstr = window.location.search.substr(1);
      return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};

function transformToAssocArray( prmstr ) {
    var params = {};
    var prmarr = prmstr.split("&");
    for ( var i = 0; i < prmarr.length; i++) {
        var tmparr = prmarr[i].split("=");
        params[tmparr[0]] = tmparr[1];
    return params;

var params = getSearchParameters();

Source: StackOverflow

iOS 9: -canOpenURL: failed for URL

If you're receiving the following error in Xcode:

-canOpenURL: failed for URL: "<scheme>://" - error: "This app is not allowed to query for scheme <scheme>"

Apple changed the canOpenURL method on iOS 9. Apps which are checking for URL Schemes have to declare these Schemes as it is submitted to Apple. More info here.

Simply open your app's .plist (usually platforms/ios/<appname>/<appname>-Info.plist ) with an editor and add the following code with your needed Schemes.


Placeholder for HTML Select box

Select boxes don't currently support the 'placeholder' attribute, so here's a workaround that works just as well.

    <option value="" disabled selected hidden>Please Choose...</option>
    <option value="0">Open when powered (most valves do this)</option>
    <option value="1">Closed when powered, auto-opens when power is cut</option>

The Disabled option stops the <option> being selected with both mouse and keyboard, whereas just using display: none allows the user to still select via the keyboard arrows. The display: none style just makes the list box look 'nice'.

Note: Using an empty value attribute on the "placeholder" option allows validation (required attribute) to work around having the "placeholder", so if the option isn't changed but is required; the browser should prompt the user to choose an option from the list.

This method is confirmed working in the following browsers:

  • Google Chrome - v.43.0.2357.132
  • Mozilla Firefox - v.39.0
  • Safari - v.8.0.7 (Placeholder is visible in dropdown but is not selectable)
  • Microsoft Internet Explorer - v.11 (Placeholder is visible in dropdown but is not selectable)
  • Project Spartan - v.15.10130 (Placeholder is visible in dropdown but is not selectable)


When the select element is required it allows use of the :invalid CSS pseudo-class which allows you to style the select element when in it's "placeholder" state. :invalid works here because of the empty value in the placeholder option.

Once a value has been set the :invalid pseudo-class will be dropped. You can optionally also use :valid if you so wish.

Most browsers support this pseudo-class. IE10+. This works best with custom styled selectelements; In some cases i.e. ( Mac in Chrome / Safari) you'll need to change the default appearance of the select box so that certain styles display i.e. background-colorcolor. You can find some examples and more about compatibility at developer.mozilla.org.

Native element appearance Mac in Chrome:

Select box native Mac in Chrome

Using altered border element Mac in Chrome:

Altered select box Mac in Chrome

Rebuild Spotlight Index on Mac OS X

Sometimes after several months Spotlight doesn't work very efficiently and starts to slow down your Mac. Fortunately there's a way to reset the Spotlight Index so that it reindexes your drives which may also lead to a smaller index file size.

Just open Terminal via Spotlight or go to Applications > Utilities > Terminal and copy in the following:

sudo mdutil -E /

This will clear everything from Spotlight and the reindex will start immediately.

If you're having problems (or suspect you're having problems) with a particular drive you can reindex only that drive by using the following command:

sudo mdutil -E /Volumes/[DriveName]

Just replace [DriveName] with the actual path or omit the path; drag and drop the drive from Finder to insert it.

You can exclude drives from Spotlight by going to System Preferences > Spotlight > Privacy.

Measuring PHP page load time

When you want to measure the actual page load time of a PHP page most developers will get the microtime at the top of the page, whilst this does measure the page load time of the scripts running on the page it doesn't accurately measure the page load time of the script.

To do so you can use $_SERVER['REQUEST_TIME_FLOAT'] to get the actual start time of the page request. Which can differ by 0.01 second or so.

$total_time = ( ( explode(' ', microtime())[0] + explode(' ', microtime())[1] ) - $_SERVER['REQUEST_TIME_FLOAT'] );

If you're just looking for a timer function then this is the most accurate way to do so.

class Timer {
	private $start;
	public function __construct() {
		$this->start = ( explode(' ', microtime())[0] + explode(' ', microtime())[1] );
	public function stop() {
		return ( explode(' ', microtime())[0] + explode(' ', microtime())[1] ) - $this->start;
	public function reset() {

$timer = new Timer();
$total_time = $timer->stop();

Avoid ad blockers with Piwik Analytics

For many reasons you may want to circumvent an Ad Blocker that's blocking your analytics script. Ad Blockers may block these scripts to curb invasive marketing tracking from third party advertisers i.e. Google. If you're using Piwik Analytics (or most other self-hosted systems) then you're in luck, you can rewrite the url to avoid the Ad Blocker.

Image: Google Analytics & Piwik Analytics requests blocked

We'll rewrite the url in .htaccess, you can change the path to anything you want. (Make sure you set the "analytics" text sections to your actual path where Piwik is installed).

RewriteEngine On
RewriteRule ^proxy-analytics/(.*) /analytics/piwik.$1 [L]

Then we'll need to update the tracking code for Piwik to work with the rewrite, here's what your code should look like before the change.

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  (function() {
    var u="//www.example.com/analytics/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 2]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
<noscript><p><img src="//www.example.com/analytics/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

And here's the lines to change: (Replace "analytics" text sections to your actual path where Piwik is installed).

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  (function() {
    var u="//www.example.com/proxy-analytics/";
    _paq.push(['setTrackerUrl', u+'php']);
    _paq.push(['setSiteId', 2]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'js'; s.parentNode.insertBefore(g,s);
<noscript><p><img src="//www.example.com/proxy-analytics/php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

Line 7: You'll need to update the url to the path that is your directory rewrite from the htaccess file.
Line 8 + 11: Remove piwik. from these lines, ad blockers may target piwik.[php/js] or like ABP they may target just the word piwik.
Line 14: Lastly, you'll need to update the path like on line 7, and again remove piwik. from the image source. The htaccess rewrite prefixes the path with piwik. so that everything still works.

Once you've made this change you should immediately be able to collect Piwik Analytics data through most Ad Blockers.

Image: Google Analytics blocked & Piwik Analytics request not blocked

This method has been tested and verified for use with 'Adblock Plus' but should work with most other Ad blockers. If you have any questions about Piwik with Ad Blockers, leave a comment or tweet me @WilliamIsted

Update: Interestingly I've just noticed that ABP is actually stopping all of the screenshot images with piwik in the name from displaying. That seems like rather aggressive filtering.

SourceGuardian - The Zend Engine API is outdated

The following error occurred on CentOS 6 with cPanel 11.48.4 when using the CLI version of PHP:

SourceGuardian requires Zend Engine API version 220131226. The Zend Engine API version 220121212 which is installed, is outdated.

Googling for a way to safely update Zend Engine API resulted in 3+ year old threads with no answers or clues on how to update with WHM installed.

If you're looking to update the Zend Engine API, then this guide is not for you. Instead, I chose to remove Zend from the CLI PHP config as the error message being displayed on all PHP crons was breaking the scripts.

Here's how to remove Zend from your PHP CLI config...

First, find the config file for the CLI PHP. You can do this by checking your existing config file using php -i – use grep to grab the path with php -i | grep 'Configuration File' .

For me php.ini was located at /usr/local/lib/php.ini

Find the line zend_extension = "/usr/local/sourceguard/ixed.x.x.lin" and comment it out. Restart Apache and you should notice that the error no longer displays. I'm not entirely sure what the Zend Engine is for under CLI PHP but I haven't encountered any issues yet.

If you encounter issues, just uncomment the line and restart Apache. Following these instructions should be a last resort if you cannot update Zend Engine API, so you do so at your own risk.

CSS hover menus with Microsoft Surface (touch device)

If you have a css hover menu you may notice that it doesn't play well with Microsoft Surface IE with either a touch pen or finger. A long press shows the menu but on releasing the menu hides again.

This is because the touch device is unaware that the hover menu is supposed to remain visible after the hover intent has ended. iOS and other touch operating systems handles this logically but the powers to be seem to require websites to be "touch optimised".

Here's what you need to do:

			<a>Hover menu</a>
				<li><a href="http://example.org">Item 1</li>
				<li><a href="http://example.org">Item 2</li>
				<li><a href="http://example.org">Item 3</li>

You'll need to make sure that your hover anchor has an href on it to make it "touchable" on certain devices, you can do this with either href="#" or href="javascript:void" , then you need to add aria-haspopup="true" to tell the device that after the user releases the tap, whatever was visible should remain visible until another interaction.

			<a aria-haspopup="true" href="#">Hover menu</a>
				<li><a href="http://example.org">Item 1</li>
				<li><a href="http://example.org">Item 2</li>
				<li><a href="http://example.org">Item 3</li>

Graceful degradation Helvetica Neue font family

Here's the tested and proven method of using Helvetica Neue fonts on your website. No Helvetica? No problem, degrades to Arial and Sans Serif for safety.

body {
	font-family: "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, sans-serif; 
	font-weight: 300;

On webkit browsers the font (along with others) may appear to have spare pixels around the lettering, making it look "fuzzy". To fix this we can tell webkit browsers to smooth the text with antialiasing:

body {
	-webkit-font-smoothing: antialiased;

This font stack originally appeared on CSS Tricks and has been modified slightly for compatibility with devices.