Hack Industries
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Latest topics
» Soiler code with css and javascript
How to insert Javascript EmptyWed Jan 27, 2010 2:45 pm by Stoned Hacker

» Forum Rules
How to insert Javascript EmptyTue Jan 26, 2010 2:07 pm by Stoned Hacker

» HTML Headings, Attributes & Paragraphs
How to insert Javascript EmptyThu Dec 10, 2009 3:45 pm by Stoned Hacker

» A Simple Virus
How to insert Javascript EmptyThu Dec 10, 2009 1:57 pm by Stoned Hacker

» Comment Box
How to insert Javascript EmptyThu Dec 10, 2009 1:53 pm by Stoned Hacker

» Create a PHP Database
How to insert Javascript EmptyThu Dec 10, 2009 1:51 pm by Stoned Hacker

» How to insert Javascript
How to insert Javascript EmptyThu Dec 10, 2009 1:12 pm by Stoned Hacker

» Intro to Javascript
How to insert Javascript EmptyThu Dec 10, 2009 12:23 pm by Stoned Hacker

» HTML Basic
How to insert Javascript EmptyThu Dec 10, 2009 12:03 pm by Stoned Hacker

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Affiliates
free forum
 

How to insert Javascript

Go down

How to insert Javascript Empty How to insert Javascript

Post  Stoned Hacker Thu Dec 10, 2009 1:12 pm

Put a JavaScript into an HTML page
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>
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>

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>
The two forward slashes at the end of comment line (//) is the JavaScript comment symbol. This prevents JavaScript from executing the --> tag.

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 in <body>
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>
Scripts in <head> and <body>
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>
Using an External JavaScript
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>
Note: Remember to place the script exactly where you normally would write the script!
Stoned Hacker
Stoned Hacker
Administrator
Administrator

Posts : 22
Join date : 2009-11-21
Age : 34
Location : The Internet

http://hackindustries.tk

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum