How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com

via How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com.

Among all cool new features introduced by HTML5, the possibility of caching web pages for offline use is definitely one of my favorites. Today, I’m glad to show you how you can create a page that will be available for offline browsing.

Getting started

1 – Add HTML5 doctype

The first thing to do is create a valid HTML5 document. The HTML5 doctype is easier to remember than ones used for xhtml:

<!DOCTYPE html>
<html>
  ...

 

Create a file named index.html, or get the example files from my CSS3 media queries article to use as a basis for this tutorial.
In case you need it, the full HTML5 specs are available on the W3C website.

2 – Add .htaccess support

The file we’re going to create to cache our web page is called a manifest file. Before creating it, we first have to add a directive to the .htaccess file (assuming your server is Apache).

Open the .htaccess file, which is located on your website root, and add the following code:

AddType text/cache-manifest .manifest

This directive makes sure that every .manifest file is served as text/cache-manifest. If the file isn’t, then the whole manifest will have no effect and the page will not be available offline.

3 – Create the manifest file

Now, things are going to be more interesting as we create a manifest file. Create a new file and save it as offline.manifest. Then, paste the following code in it. I’ll explain it later.

CACHE MANIFEST
#This is a comment

CACHE
index.html
style.css
image.jpg
image-med.jpg
image-small.jpg
notre-dame.jpg

Right now, you have a perfectly working manifest file. The way it works is very simple: After the CACHE declaration, you have to list each files you want to make available offline. That’s enough for caching a simple web page like the one from my example, but HTML5 caching has other interesting possibilities.

For example, consider the following manifest file:

CACHE MANIFEST
#This is a comment

CACHE
index.html
style.css

NETWORK:
search.php
login.php

FALLBACK:
/api offline.html

Like in the example manifest file, we have a CACHE declaration that caches index.html and style.css. But we also have the NETWORK declaration, which is used to specify files that shouldn’t be cached, such as a login page.

The last declaration is FALLBACK. This declaration allows you to redirect the user to a particular file (in this example, offline.html) if a resource (/api) isn’t available offline.

4 – Link your manifest file to the html document

Now, both your manifest file and your main html document are ready. The only thing you still have to do is to link the manifest file to the html document.

Doing this is easy: simply add the manifest attribute to the html element as shown below:

<html manifest="/offline.manifest">

5 – Test it

Once done, you’re ready to go. If you visit your index.html file with Firefox 3.5+, you should see a banner like this one:

Other browser I’ve tested (Chrome, Safari, Android and iPhone) do not warn about the file caching, and the file is automatically cached.

Below you’ll find the browser compatibility of this technique: As usual Internet Explorer does not support it.

  • IE: No support
  • Firefox: 3.5+
  • Safari: 4.0+
  • Chrome: 5.0+
  • Opera: 10.6+
  • iPhone: 2.1+
  • Android: 2.0+
Advertisements

39 comments on “How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com

  1. Quite superior article. We basically discovered your website and desired to say that We have really liked studying your blog and posts. Anyways I’ll be following your feed and I hope to study your blog again.

  2. I used to be very pleased to seek out this internet-site. I needed to thanks for your time for this glorious learn!! I positively having fun with every little little bit of it and I have you bookmarked to take a look at new stuff you weblog post.

  3. Thankyou for sharing How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird with us keep update bro love your article about How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird .

  4. Thankyou for sharing How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird with us keep update bro love your article about How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird .

  5. Thanks for sharing How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird with us keep update bro love your article about How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird .

  6. Hey there, I want to ask you some thing. Is this site a wordpress website? I will be thinking of moving my weblog from Blogger to wordpress, ya think this is doable? In addition did you construct this particular theme yourself some how? Many thanks for the help!

  7. Thanks for sharing How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird with us keep update bro love your article about How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird .

  8. Regards for sharing How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird with us keep update bro love your article about How to create offline HTML5 web apps in 5 easy steps | CatsWhoCode.com Codingbird .

  9. It’s a new pity you actually don’t have a very donate switch! I’d most likely donate to this superb web site! My spouse and i suppose that for the moment i’ll be happy with book-marking along with incorporating your current Rss feed for you to our Yahoo and google bill. My spouse and i search forth for you to unique posts all of which will show this specific web log using our Zynga group: )

  10. I have to say, while looking through hundreds of blogs daily, the theme of this blog is different (for all the proper reasons). If you do not mind me asking, what’s the name of this theme or would it be a especially designed affair? It’s significantly better compared to the themes I use for some of my blogs.

  11. It is critical, hence, to make sure that any kind of untrusted subject matter the fact that varieties section of a web site get organised for a several starting point as compared to every vulnerable written content concerning in which webpage. Untrusted content can certainly spoof each and every web site for a passing fancy foundation, look over information as a result of which source, result in scripts in this source in order to perform, submit varieties that will as well as via in which foundation even if they are protected by cross-site inquire forgery blasts by simply distinct bridal party, and even work with any third-party options exposed to or perhaps protection under the law naturally to the next basis

  12. Pingback: Subliminal Advertising

  13. Hi there, I found your blog via Google while searching for a related topic, your web site came up, it looks great. I have bookmarked it in my google bookmarks.

Comments are closed.