SHARE
Facebook X Pinterest WhatsApp

Accessing User’s Address book using Contacts Intent in HTML5

Written By
thumbnail
Vipul Patel
Vipul Patel
Aug 30, 2013

Introduction

The Contacts Intent section in HTML5 specification http://www.w3.org/TR/contacts-api/ was an attempt to a uniform view to the contacts; an implementation which varied across multiple operating systems and web applications. Users had to maintain multiple sets of same address book information since there was nothing available to tie it together, resulting in redundant and often stale information.

The Contacts Intent specifies that a web application can access a subset of a user’s address book. This subset can be obtained from sources unknown to the web application, and supports user’s security and privacy settings.

The sources of the address book data need to be registered as Intent services, which allows web application to access the data in a consistent manner.

The support for accessing Contacts initially came in as JavaScript APIs in the initial version of HTML5 specification, but as the specification matured, it morphed into a web intent. The intent version still attempts to stay true to the API behavior it replaced.

The Contact dictionary in defined as:

dictionary Contact {
  DOMString       id;
  DOMString?      displayName;
  ContactName?     name;
  DOMString?      nickname;
  ContactField[]?    phoneNumbers;
  ContactField[]?    emails;
  ContactAddress[]?   addresses;
  ContactField[]?    ims;
  ContactOrganization[]? organizations;
  Date?         birthday;
  DOMString?      note;
  ContactField[]?    photos;
  DOMString[]?     categories;
  ContactField[]?    urls;
}; 

 

The syntax for intent is simple. You declare the action you want to perform and the type on which you want to perform the intent on. In the extras, you specify the fields you need access to.

For example,

var contactIntent = new Intent({ action: “http://webintents.org/pick”,

type: “http://w3.org/type/contact”,

extras: { fields: [“displayName”, “phoneNumbers”] }});

 

Next, you specify your success and failure callback methods in your intent invocation call.

navigator.startActivity(contactIntent, contactsSuccess, contactsFail);

 

When the address book data is retrieved successfully, the contactsSuccess method will be called, if the retrieval is a failures, contactsFail callback will be called.

 

The complete HTML source will look as under:

 <!DOCTYPE html>

<html>

<meta charset=”utf-8″>

<title>Contacts Javascript sample</title>

<body>

<button id=”button1″ type=”button” onclick=”button1_click()”>Click Me!</button>

<script type=”text/javascript”>

function contactsSuccess (contacts)

{

// iterate over the array of contacts to do something useful with them

alert(‘ContactsSuccess API called!’);

}

function contactsFail (err)

{

// display an error to the user

alert(‘ContactsFail API called!’);

}

function button1_click()

{

alert(‘Button clicked! Registering contact intent’);

var contactIntent = new Intent({ action: “http://webintents.org/pick”,

type: “http://w3.org/type/contact”,

extras: { fields: [“displayName”, “phoneNumbers”] }});

navigator.startActivity(contactIntent, contactsSuccess, contactsFail);

}

</script>

</body>

</html>

 

The sample HTML file containing this code is available for download at <link>.

Today, the browsers (like Google Chrome) do not support registering the Intent. You can work-around it by registering an extension (see http://www.chromium.org/developers/web-intents-in-chrome for details).

Once you have registered the Contacts Intent successfully, you can view our page in a HTML5 compatible browser and see that only restricted access to the address book is granted to the web application. Upon loading the webpage, you will be asked to select from an address book. (by opening the service page, which will allow to select contacts.

 

Summary

In this article, we learned about the Contacts Intent and how to build a web application which can request access to address book contacts. I hope you have found this information useful.

 

Recommended for you...

Best VR Game Development Platforms
Enrique Corrales
Jul 21, 2022
Best Online Courses to Learn HTML
Ronnie Payne
Jul 7, 2022
Working with HTML Images
Octavia Anghel
Jun 30, 2022
Web 3.0 and the Future Of Web Development
Rob Gravelle
Jun 23, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.