Monday, April 4, 2011

Javascript example!

Would you like a very simple example of Javascript in action? No problem. Let's say you have a page and when you click a button, you'd like to execute a Javascript function to update some text in the page to display the current date / time. Now first let's see that in action!

Just press the button...

This will be updated with the date and current time!



Now the source code...

This will be updated with the current date and time!






Now here are the salient points:
  1. The Javascript function is called displayDate() and this is defined in the head.
  2. The paragraph in the html page is given the id=demo.
  3. The Javascript function gets a handle on the demo paragraph using the DOM APIs
  4. The Javascript function updates the paragraph using the innerHtml API.

And the source code for this baby...

No comments:

Post a Comment