Add Twitter Cards To Your Website

When you share a post on a social network like Facebook it will scan the URL that you are sharing and return certain information to display the page in your feed. Facebook will get the page title, page description and a thumbnail image for the page. You can change the information that Facebook gets by using the open graph meta tags.

These are simply meta tags you can add to the head tag of your page, when Facebook scans your page to get the title, description and images it will search for these tags, if they are there then it will use the contents of these tags instead of the page title. These means you can fully customise the page titles just for Facebook.

card-web-summary_0

Twitter also has meta tags which you can use to customise the message on Twitter. These are called Twitter cards, it allows website owners to add more descriptive text on their links when they are shared on Twitter.
Continue reading »

Always Get A Checkbox $_POST Value

The problem with checkboxes is that if they are not checked then they are not posted with your form. If you check a checkbox and post a form you will get the value of the checkbox in the $_POST variable which you can use to process a form, if it's unchecked no value will be added to the $_POST variable.

In PHP you would normally get around this problem by doing an isset() check on your checkbox element. If the element you are expecting isn't set in the $_POST variable then we know that the checkbox is not checked and the value can be false.

if(!isset($_POST['checkbox1']))
{
     $checkboxValue = false;
} else {
     $checkboxValue = $_POST['checkbox1'];
}

But if you have created a dynamic form then you won't always know the name attribute of your checkboxes, if you don't know the name of the checkbox then you can't use the isset function to check if this has been sent with the $_POST variable.

To get around this problem you can use a new hidden element which will store the true value of the checkbox. What you need to do is create a hidden element and use Javascript to change the value of the hidden field to the true value of the checkbox.
Continue reading »

Understanding The Viewport Meta Tag

When working on a new web design one of things you need to think about is responsive design. Is the website you are about to make going to need a responsive design?

I think all external facing sites should take responsive design into consideration. There are some people who think that responsive design is not worth it as people on mobile devices can zoom in to see what they want, but I feel developers should make a website easy to use on any device.

What Is Responsive Design?

Responsive design is when your website design can adapt to the width and height of the device it is being viewed on. Responsive design is done by using media queries in your CSS file to change the styling of your HTML elements depending on certain breakpoints you setup. Just adding a simple width:100% to some elements is enough to make them responsive when viewed on mobile devices.

The breakpoints define a range of widths which will use different CSS styles. This is typically different sizes of devices such as mobile phones, 7 inch tablets, 10 inch tablets, 13 inch laptops and desktop monitors.

You can define the breakpoints as either pixel widths or min device pixel ratio.
Continue reading »

How To Use The HTML5 Notification API

HTML5 isn't something new, everyone has heard about it but there are some features with HTML5 that people don't know about. Most people think that all HTML5 is are new semantic tags like header, footer, nav, article, section which you can use on the document but many people don't know some of the awesome new features which are now available to developers, things like:

In this article we are going to look at a new feature in HTML5 called the notification API.

The most popular use of this feature would be Gmail alerting you of new email, if you have a Gmail tab currently open. You will get an alert box pop up at the bottom right of the screen with the title and message inside. We are going to look at how you can use this API to create your own notification messages.

Browser Support

Currently only supported on Chrome 19+ and Safari 6+.

A good place to start is to mention the browser support, the notification API is still in draft stage and therefore it is not a current web standard. This means that it is not supported over all browsers, in fact this is only supported on webkit browsers.

It was first introduced into webkit from version 19 of Chrome and version 6 of safari. It is not yet known if the other browsers will be supporting this in the future but I think they should as I can see it being a very important feature for web apps.

The best place to see what browsers support this feature is to use the website caniuse.com.

Notification API

Notification API

There are two objects to use in the Notication API the first is the notification object.

interface Notification : EventTarget {
 // display methods
 void show();
 void cancel();
 // event handler attributes
 attribute Function ondisplay;
 attribute Function onerror;
 attribute Function onclose;
 attribute Function onclick;
}

This is the interface you will use for the notifications, it has 2 methods and 4 attributes attached to it.

Notification Methods

The notification methods are used to display or hide the notification box.

  • Show - This method will display a notification
  • Cancel - This will remove the notification, if the notification is currently displayed this will hide it. If the notification is not displayed then this will stop it from being displayed.

Notification Attributes

The notification attributes are used as event listeners on the different events on the notification.

  • ondisplay - A function to run when the notification box is displayed.
  • onerror - A function to run when there is an error with the notification box.
  • onclose - A function to run when the notification box is closed.
  • onclick - A function to run when there is a click on the notification box.

The second object needed for notifications is the NotificationCenter interface which is available to the webpage via the window interface.

interface NotificationCenter {
 // Notification factory methods.
 Notification createNotification(in DOMString iconUrl, in DOMString title, in DOMString body) throws(Exception);
 optional Notification createHTMLNotification(in DOMString url) throws(Exception);
 // Permission values
 const unsigned int PERMISSION_ALLOWED = 0;
 const unsigned int PERMISSION_NOT_ALLOWED = 1;
 const unsigned int PERMISSION_DENIED = 2;
 // Permission methods
 int checkPermission();
 void requestPermission(in Function callback);
}
interface Window {
...
 attribute NotificationCenter webkitNotifications;
...
}

The notification center is used to create notification objects and check that the webpage has permission to display the notification objects.

There are 4 methods that will need to use on the notification center:

  • createNotification - If the notification has permission to be displayed then this method will create a notification object populated with the content supplied to it. If the web page does not have permission to display the notification then this will throw a security exception.
  • createHTMLNotification - This is similar to the createNotification method it will return a populated notification if the webpage has permission to display the notification. This method uses a parameter of a URL to get the HTML to be displayed.
  • checkPermission - This will return an integer of the permission the notification has on this webpage. PERMISSION_ALLOWED = 0, PERMISSION_NOT_ALLOWED = 1, or PERMISSION_DENIED = 2
  • requestPermission - This will ask for permission from the user to display the notification box.

Does The Browser Support Notifications

If you are going to do anything with the notification API you first need to check if the browser supports it to do this you can use a simple if statement on webkitNotification.

/**
 * Check if the browser supports notifications
 *
 * @return true if browser does support notifications
 */
 function browser_support_notification()
 {
        return window.webkitNotifications;
 }

Get Permissions To Display Alert

To display notifications to the user you must first get permission to display any notifications. To get permission to display all you have to do is use the method requestPermission().

/**
* Request notification permissions
*/
function request_permission()
{
	// 0 means we have permission to display notifications
	if (window.webkitNotifications.checkPermission() == 0) {
	    window.webkitNotifications.createNotification();
        } else {
	    window.webkitNotifications.requestPermission();
	}
}

Display Plain Text Notification Box

To create a plain text notification box you first need to check if you have permission to display the notification box. If you do then we can create a new notification using the method createNotification() passing in the parameters for the image, title and description.

/**
* Create a plain text notification box
*/
function plain_text_notification(image, title, content)
{
    	if (window.webkitNotifications.checkPermission() == 0) {
    		return window.webkitNotifications.createNotification(image, title, content);
    	}
}

Display HTML Notification Box

To display HTML in a notification box we need to do the same thing as the plain text notification box by checking if we have permission to display the notification box. If we do then we can create the a notification box passing in the HTML URL to display.

/**
 * Create a notification box with html inside
 */
 function html_notification(url)
 {
    	if (window.webkitNotifications.checkPermission() == 0) {
    		return window.webkitNotifications.createHTMLNotification(url);
    	}
 }

Demo

On the demo page you will see this notification alert box in action, both the plain text and html demo.

Please note that for the demo to work it's best to use Chrome.

Demo

How To Use HTML5 GeoLocation API With Google Maps

In this tutorial we are going to learn how to use the Geolocation API to get your current latitude and longitude, from these results we can then connect to the Google Maps API to display your location in the browser.

Google Map

Demo

The Geolocation API

The Geolocation API defines an interface used to get location information of the visitor, from this interface you can return the latitude and longitude of the current visitor. Common sources of the location are GPS or IP Address.
Continue reading »

What Is HTML5?

HTML5
HTML5 is the next version of HTML. This will introduce some brand new features which will make building HTML even easier. This is by introducing features which to make your website layout clearer to both coders and search engines.

This can help search engines because of the header, footer, nav and article tags. These are newly introduced tags which define the main areas of a website. If you can tell the search engines that the nav will hold links to navigate around the site then they can use all the links in this section. The article tag is probably the most important tag, as content is the most important part of a page you can let the search engines know that this area is where all the content is.

Apart from the new tags which are included in HTML5 it also introduces a feast of APIs which allow you to make graphical drawings, store data offline and drag and drop.

Getting Started With HTML5

Getting started using HTML5 is very easy and it's using my favourite of the new HTML5 features the Doctype.
Continue reading »