Latest topics
How to insert Javascript
Page 1 of 1
How to insert Javascript
Put a JavaScript into an HTML page
The example below shows how to use JavaSript to write text on a web page:
EXPLANATION:
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, the <script type="text/javascript"> and </script> 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 <script> and </script> 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:
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 <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, like this:
Where to Put the JavaScript
JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered, go in the head section.
If you place a script in the head section, you will ensure that the script is loaded before anyone uses it.
Scripts to be executed when the page loads go in the body section.
If you place a script in the body section, it generates the content of a page.
You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
If you want to run the same JavaScript on several pages, without having to write the same script on every page, you can write a JavaScript in an external file.
Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the <script> tag!
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
The example below shows how to use JavaSript to write text on a web page:
- Code:
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
- Code:
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
EXPLANATION:
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, the <script type="text/javascript"> and </script> 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 <script> and </script> 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:
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 <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, like this:
- Code:
<html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>
Where to Put the JavaScript
JavaScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event.
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered, go in the head section.
If you place a script in the head section, you will ensure that the script is loaded before anyone uses it.
- Code:
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body>
</html>
Scripts to be executed when the page loads go in the body section.
If you place a script in the body section, it generates the content of a page.
- Code:
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html>
You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
- Code:
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>
If you want to run the same JavaScript on several pages, without having to write the same script on every page, you can write a JavaScript in an external file.
Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the <script> tag!
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
- Code:
<html>
<head>
<script type="text/javascript" src="xxx.js"></script>
</head>
<body>
</body>
</html>
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Wed Jan 27, 2010 2:45 pm by Stoned Hacker
» Forum Rules
Tue Jan 26, 2010 2:07 pm by Stoned Hacker
» HTML Headings, Attributes & Paragraphs
Thu Dec 10, 2009 3:45 pm by Stoned Hacker
» A Simple Virus
Thu Dec 10, 2009 1:57 pm by Stoned Hacker
» Comment Box
Thu Dec 10, 2009 1:53 pm by Stoned Hacker
» Create a PHP Database
Thu Dec 10, 2009 1:51 pm by Stoned Hacker
» How to insert Javascript
Thu Dec 10, 2009 1:12 pm by Stoned Hacker
» Intro to Javascript
Thu Dec 10, 2009 12:23 pm by Stoned Hacker
» HTML Basic
Thu Dec 10, 2009 12:03 pm by Stoned Hacker