mercredi 24 juillet 2013

Embedding JavaScript code in HTML documents

javaScript code can be included in simple html documents. To enable browser to differentiate between html and Javascript code, JS code is placed within script tags.

OPENING TAG <script type = "text/javascript">

Javascript commands go between these tags.

ENDING TAG </script>
Javascript in HTML Example

In this example we will use a very basic Javascript command called 'document.write' . This command is used to output/display some text, number, result or value of a variable on screen. For example, Browser calculates the price after 10% discount. In order to display that value on the screen, document.write command has to be used. We will however use a very simple output for this example.

<html>
<head>
<title> My first Javascript Page </title>

<script type="text/javascript">

document.write("Welcome to my first page");

</script>
</head>
<body>

</body>
</html>

In the example above, you can see how we can insert javascript within an html document. This simple document.write code will output/display the text on the screen of the browser.

NOTE: Some users prefer to put Javascript coding within the html <head> </head> tags , while some prefer body tags. You can place it in either location or also some code in head while other bit of code in the body.
Result- How to write Javascript in html

Here is how it will appear in the browser

Javascript code in html page

Apart from writing JAVASCRIPT in html documents, it can also be put it a separate file. 

Aucun commentaire:

Enregistrer un commentaire