Put a Javascript into an HTML page
(The example below shows how to use Javascript to write text on a web page:)
The example below shows how to add HTML tags to the Javascript:
Example Explained[/br]
To insert a Javascript into an HTML page, we use the tag. Inside the tag we use the type attribute to define the scripting language.
So, the and tells where the Javascript starts and ends:
The document.write command is a standard Javascript command for writing output to a page.
By entering the document.write command between the and tags, the browser will recognize it as a Javascript command and execute the code line. In this case the browser will write Hello World! to the page:
[b]How to Handle Simple Browsers
Browsers that do not support Javascript, will display Javascript as page content.
To prevent them from doing this, and as a part of the Javascript standard, the HTML comment tag should be used to "hide" the Javascript.
Just add an HTML comment tag (end of comment) after the last Javascript statement, like this:
The two forward slashes at the end of comment line (//) is the Javascript comment symbol. This prevents Javascript from executing the --> tag.
(The example below shows how to use Javascript to write text on a web page:)
- Code:
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
The example below shows how to add HTML tags to the Javascript:
- Code:
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
Example Explained[/br]
To insert a Javascript into an HTML page, we use the
So, the
- Code:
<html>
<body>
<script type="text/javascript">
...
</script>
</body>
</html>
The document.write command is a standard Javascript command for writing output to a page.
By entering the document.write command between the
- Code:
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
[b]How to Handle Simple Browsers
Browsers that do not support Javascript, will display Javascript as page content.
To prevent them from doing this, and as a part of the Javascript standard, the HTML comment tag should be used to "hide" the Javascript.
Just add an HTML comment tag (end of comment) after the last Javascript statement, like this:
- Code:
<html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>
The two forward slashes at the end of comment line (//) is the Javascript comment symbol. This prevents Javascript from executing the --> tag.