Saturday, May 28, 2011

JAVASCRIPT EXAMPLES

Previously I posted the definition of javascript. Now I'm giving an example on how to put javascript to the html document.

The HTML <script> tag is used to insert a JavaScript into an HTML page.

The example below writes a <p> element with current date information to the HTML document:

<html>
<body>

<h1>My First Web Page</h1>

<script type="text/javascript">
document.write("<p>" + Date() + "</p>");
</script>


</body>
</html>

Output:


Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.

To insert a JavaScript into an HTML page, use the <script> tag. 

Inside the <script> tag use the type attribute to define the scripting language.

The <script> and </script> tells where the JavaScript starts and ends:



 The lines between the <script> and </script> contain the JavaScript and are executed by the browser. 

For more information: w3schools

No comments: