Tuesday 19 November 2013

How do I write a JavaScript?

How do I write a JavaScript?

Question

How do I write a JavaScript?

Answer

To write a JavaScript, you will need a Web browser and either a text editor or an HTML editor.
  • For the Web Browser, you could use Microsoft Internet Explorer, Mozilla Firefox, Google Chrome, or any modern Web browser you prefer.
  • For the text/HTML editor, you can use any number of options such as NotepadWordpad, Dreamweaver, or one of the numerous other editors available.
Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML document, you will need to create or open an HTML file with your text/HTML editor. A basic HTML file will have a docType and some basic HTML tags such as <html> <head> and <body>. For example, a basic HTML 5 document might look something like what is shown below.
<!DOCType HTML>
<html>
<head>
<title>Testing JavaScript</title>
</head>
<body>
Content goes here...
</body>
</html>
When you see JavaScript code on the Web, you will sometimes see some JavaScript code between the <head></head> tags, or you may see it within the <body></body> tags (or even in both places). To separate JavaScript code from HTML code, you will need to enclose it within a set of <script></script> tags. The opening <script> tag has one required attribute and one optional attribute. The required attribute is the type attribute, while the optional attribute is src (which allows you to point to an external script file, covered later in this answer). The value of the type attribute is set totext/javascript, as shown below.
<script type="text/javascript">
Your JavaScript code will go here...
</script>
As you can see, your JavaScript code will be placed between the opening and closing script tags. As an example script, you could write a simple string of text directly on the Web page, as shown in the code below (place this between the <body> and </body> tags):
<script type="text/javascript">
document.write("This text was written using JavaScript code!");
</script>
This uses a standard JavaScript function to write the text between the quotation marks on the page. It would look like the below example.
This text was written using JavaScript code!
Another option for including a JavaScript on a page is to create the script in an external text file and save it with a .js extension (making it a JavaScript file). This file is then included in the page using the src attribute of the opening script tag. So, if you wanted to use the script above by placing it in an external file, you would place the JavaScript code (without the script tags) into a new text file, as shown below:
document.write("This text was written using JavaScript code!");
You would then save the file with a .js extension. For instance, you could save it as write.js. Once the file has been saved, you can call it from the HTML code via the src attribute of the opening script tag, as shown below for write.js.
<script type="text/javascript" src="write.js"></script>
This will have the same effect as writing the code between the script tags, but won't clutter the HTML code with JavaScript code (which can become quite lengthy at times). Another advantage to this method is that the same script can be included in multiple pages, and editing the script file will update the script in every page that uses the external script file. This makes it far easier to edit the script as it can be done in one place (rather than editing the code within the script tags on each page that contains the  script

No comments:

Post a Comment