With application cache it is easy to make an offline version of a web application, by creating a cache manifest file.
HTML5 introduces application cache, which means that a web application is cached, and accessible without an internet connection.
Application cache gives an application three advantages:
The numbers in the table specify the first browser version that fully supports Application Cache.
API |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
Application Cache | 4.0 | 10.0 | 3.5 | 4.0 | 11.5 |
The example below shows an HTML document with a cache manifest (for offline browsing):
To enable application cache, include the manifest attribute in the document's <html> tag:
Every page with the manifest attribute specified will be cached when the user visits it. If the manifest attribute is not specified, the page will not be cached (unless the page is specified directly in the manifest file).
The recommended file extension for manifest files is: ".appcache"
![]() |
A manifest file needs to be served with the correct media type, which is "text/cache-manifest". Must be configured on the web server. |
---|
The manifest file is a simple text file, which tells the browser what to cache (and what to never cache).
The manifest file has three sections:
The first line, CACHE MANIFEST, is required:
The manifest file above lists three resources: a CSS file, a GIF image, and a JavaScript file. When the manifest file is loaded, the browser will download the three files from the root directory of the web site. then, whenever the user is not connected to the internet, the resources will still be available.
The NETWORK section below specifies that the file "login.asp" should never be cached, and will not be available offline:
An asterisk can be used to indicate that all other resources/files require an internet connection:
The FALLBACK section below specifies that "offline.html" will be served in place of all files in the /html/ catalog, in case an internet connection cannot be established:
Note: the first URI is the resource, the second is the fallback.
Once an application is cached, it remains cached until one of the following happens:
![]() |
Tip: lines starting with a "#" are comment lines, but can also serve another purpose. An application's cache is only updated when its manifest file changes. If you edit an image or change a JavaScript function, those changes will not be re-cached. Updating the date and version in a comment line is one way to make the browser re-cache your files. |
---|
Be careful with what you cache.
Once a file is cached, the browser will continue to show the cached version, even if you change the file on the server. To ensure the browser updates the cache, you need to change the manifest file.
Note: browsers may have different size limits for cached data (some browsers have a 5MB limit per site).
A web worker is a JavaScript running in the background, without affecting the performance of the page.
When executing scripts in an HTML page, the page becomes unresponsive until the script is finished.
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., while the web worker runs in the background.
The numbers in the table specify the first browser version that fully support Web Workers.
API |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
Web Workers | 4.0 | 10.0 | 3.5 | 4.0 | 11.5 |
The example below creates a simple web worker that count numbers in the background:
Before creating a web worker, check whether the user's browser supports it:
Now, let's create our web worker in an external JavaScript.
Here, we create a script that counts. the script is stored in the "demo_workers.js" file:
The important part of the code above is the postMessage() method - which is used to post a message back to the HTML page.
Note: Normally web workers are not used for such simple scripts, but for more CPU intensive tasks.
Now that we have the web worker file, we need to call it from an HTML page.
The following lines checks if the worker already exists, if not - it creates a new web worker object and runs the code in "demo_workers.js":
Then we can send and receive messages from the web worker.
Add an "onmessage" event listener to the web worker.
When the web worker posts a message, the code within the event listener is executed. the data from the web worker is stored in event.data.
When a web worker object is created, it will continue to listen for messages (even after the external script is finished) until it is terminated.
To terminate a web worker, and free browser/computer resources, use the terminate() method:
If you set the worker variable to undefined, after it has been terminated, you can reuse the code:
We have already seen the Worker code in the .js file. Below is the code for the HTML page:
Since web workers are in external files, they do not have access to the following JavaScript objects:
Server-Sent Events allow a web page to get updates from a server.
A server-sent event is when a web page automatically gets updates from a server.
this was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.
Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.
The numbers in the table specify the first browser version that fully support server-sent events.
API |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
SSE | 6.0 | Not Supported | 6.0 | 5.0 | 11.5 |
The EventSource object is used to receive server-sent event notifications:
Example explained:
In the tryit example above there were some extra lines of code to check browser support for server-sent events:
For the example above to work, you need a server capable of sending data updates (like PHP or ASP).
The server-side event stream syntax is simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending event streams.
Code in PHP (demo_sse.php):
Code in ASP (VB) (demo_sse.asp):
Code explained:
In the examples above we used the onmessage event to get messages. But other events are also available:
Events | Description |
---|---|
onopen | When a connection to the server is opened |
onmessage | When a message is received |
onerror | When an error occurs |