Easter Calculator

We can add any JavaScript that we like to a web page in an unobtrusive way. The easter calculator script will add a form into your page that will allow anyone with a JavaScript enabled browser to calculate when Easter day falls in any year.

This script can be added unobtrusively to any web page simply by including the appropriate script and link tags in the head of your page in order to link the JavaScript to your page. The only other change needed is to add the form into your web page.

In this particular instance we have a form that will always appear in the page and so we need a way for the form to work even if JavaScript is disabled. In this particular case since most web hosts support PHP I have made the page a PHP page rather than an HTML one. The form contains both an action that will reload the page and defines that the method to use to pass the form fields to the reloaded page is 'post'. The input field within the form is set up with a name so that the field can be posted. We also use a sumbit button to ensure that the form can actually be submitted. The PHP that is added to the source duplicates the Easter calculation (only using PHP instead of JavaScript) if a year was posted to the page (indicating that the form has already been submitted). The only other PHP required is to echo the three values in the form.

With the PHP in place the form is functional but requires reloading of the page to calculate when Easter will be. When JavaScript is supported the unobtrusive JavaScript will override the submit process to calculate and update the form directly without reloading the page. This satisfies both requirements of unobtrusive javaScript - that the page be functional without the JavaScript and that the JavaScript be in a completely separate file.

Download a complete copy of the Easter Calculator including sample PHP that shows you how to attach this unobtrusive JavaScript into your page.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow