How To Put a JavaScript into an HTML Page

Leave a Comment

The following example shows how to use JavaScript to write text on a Web page.
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
Here’s your first opportunity to personalize JavaScript. Change the “Hello World” text to “Happy, Happy, Joy, Joy!” and see what happens.
<html>
<body>
<script type="text/javascript">
document.write("Happy, Happy, Joy, Joy!");
</script>
</body>
</html>
The following example shows how to add HTML tags to the JavaScript.
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
Continuing with our happier version of the code, change the “Hello World!” text to “Happy, Happy, Joy, Joy!” and see what happens.
<html>
<body>
<script type="text/javascript">
document.write("<h1>Happy, Happy, Joy, Joy!</h1>");
</script>
</body>
</html>
To insert a JavaScript into an HTML page, we use the <script> tag. Inside the <script> tag we use the type attribute to define the scripting language. So, <script type="text/javascript"> and </script> tell where the Java- Script starts and ends:
<html>
<body>
<script type="text/javascript">
...
</script>
</body>
</html>
The document.write command is a standard JavaScript command for writing output to a page.

When you type the document.write command between the <script> and </script> tags, the browser will recognize it as a JavaScript command and execute the code line. In this case, the browser writes Hello World! to the page:
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>

0 comments:

Post a Comment