Create a "Give me directions" button using jQuery and HTML5 Geolocation

17 May 2013, Danny Connell

An upcoming mobile website required the inclusion of a "Give me directions" button. I.E. a button that the mobile user can click to see directions from their current location to a given destination.

Here is my simple custom solution.

View demo page | Download demo page


Firstly, I added a button to the page for the user to click:

<a href="#" class="get-directions">Give me directions to Reading Room Manchester</a>


I included jQuery, and began by creating variables for the starting location and destination:

$(document).ready(function() {

var startingLocation;
var destination = "reading+room+manchester"; // replace this with any destination


Next I attach a click event to my button:

$('a.get-directions').click(function (e) {


Within this click function, I use HTML5 geolocation to calculate the user's latitude and longitude, combine these two values into a string, and send the starting location and destination to a function:

// check if browser supports geolocation
if (navigator.geolocation) {

// get user's current position
navigator.geolocation.getCurrentPosition(function (position) {

// get latitude and longitude
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
startingLocation = latitude + "," + longitude;

// send starting location and destination to goToGoogleMaps function
goToGoogleMaps(startingLocation, destination);



Finally, within the goToGoogleMaps function, I send the user to Google Maps, along with the query needed to get the directions:

// go to Google Maps function - takes a starting location and destination and sends the query to Google Maps
function goToGoogleMaps(startingLocation, destination) {
window.location = "" + startingLocation + "&daddr=" + destination;

Fallback for browsers without Geolocation

What if the user's browser doesn't support Geolocation? Or they selected 'no' to the prompt asking to use their location? I wanted it to still work. Upon clicking the button, the user would be presented with a warning, along with a text input. The user could then manually enter their postcode, click the button and still get directions.

Here is the HTML for the warning and input (this is hidden by default in the CSS):

<div class="no-geolocation">
<p class="warning">Your browser does not support Geolocation. Please enter your postcode and click the button again.</p>
<input type="text" placeholder="Enter postcode or address" class="manual-location">

And here is the JavaScript for the fallback:

// get manually entered postcode
startingLocation = $('.manual-location').val();

// if user has entered a starting location, send starting location and destination to goToGoogleMaps function
if (startingLocation != '') {
goToGoogleMaps(startingLocation, destination);
// else fade in the manual postcode field
else {

View demo page | Download demo page

Read more

Right click > "Add new website template" in Windows 7

1 August 2012, Danny Connell

      Like many developers, I have a website template that I use as a starting point for every web project I do, whether it's a client project, or just a quick demo page.

This is made up of an index.html file, along with a folder named assets which contains folders for css, fonts, images and js and some respective files therein.

I find myself using this template more and more frequently, and every time I must: open Explorer, navigate to my template folder, then copy and paste it to the location I'm currently working in.

Bit of a ballache!

Wouldn't it be great if I could just right click anywhere and have an option to create a new website folder right there?

Here's how!

  • Press Win Key, type regedit and hit Enter to launch the registry editor
  • Navigate to HKEY_CLASSES_ROOT > Directory > Background > shell
  • Right click on shell and choose New > Key
  • Enter the name NewWebsiteTemplate and hit Enter
  • On the right hand pane, double click (Default)
  • In the text field, enter New website template (or whatever you want it to say on the right-click menu).
  • Right click on NewWebsiteTemplate and choose New > Key
  • Enter the name Command and hit Enter
  • In the right-hand pane, double click (Default)
  • Enter the following text, replacing the first path with the path where you keep your template (I keep mine in a dropbox subfolder so that I always have an up-to-date copy wherever I am):
    xcopy "C:\Dropbox\website template" "%V\New website template\" /E
  • Hit Enter

Registy editor - Right click > Add new website template in Windows 7

And that's it! Alternatively, you can save the following file, open it, change the first path to the path of your template, save it and double click it to apply the registry changes.

Download registry file

If anyone can make this better and add the option to the right click > New menu, please shout up!

Read more

Notify - simple, lightweight notifications using jQuery

29 June 2012, Danny Connell

I'm currently working on a project with tonnes of advanced jQuery functionality where performance is of utmost importance. Therefore I'm avoiding the use of plugins and keeping my code minimal and optimized.

I needed a simple way to display four types of notification (information, success, errors and warnings).

There are several plugins available that are fit for this purpose, but I needed something ultra-lightweight and ultra-fast.

So I knocked up Notify. To use it on your page, you just need to add 12 CSS rules, a single JavaScript function and no images.

To make a notification appear, just use a single line of JavaScript like this one:

notify('Your information was saved successfully!', 'success');

Check out the Notify page

Read more