Adding JavaScript in an HTML page

To insert JavaScript into an existing HTML document, it is necessary to introduce the <script> </script> tag. Inside the <script> ... </script> element we write our JS code.

To write and execute JavaScript programs we need a simple text editor (such as Windows Notepad, emacs Unix or Notepad++) and a browser (Mozilla Firefox, Chrome, Internet Explorer).

We can also place the JS instructions in a separate JavaScript file, external, that will have the '.js' extension. To edit this file is needed only a simple text editor, like Notepad. The advantage of an external file is that we can use the same code in multiple HTML pages in case of a necessary change in the JavaScript code, we just need to modify the data in a single file (the one with the extension 'js').
In the '.js' external file we can't use HTML tags.

Here is a sample JavaScript script written within a web page (HTML):
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>Example simple JavaScript code</h4>

<script>
document.write('Text from JavaScript displayed in web page.');
</script>
</body>
</html>
- The document.write instruction is used to print something on the page.

• If you want to load the script from an external file (eg, 'code.js '), our code in the HTML document will look like:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>Example simple JS code</h4>

<script src='code.js'></script>
</body>
</html>
- The 'code.js' contains:
document.write('Text from JavaScript displayed in web page.');

Syntax conventions

In any language, the code has conventions and rules of syntax.
Now it's presented the JavaScript syntax rules:

  1. Case-sensitive - It makes distinction between large and small letters, such words as 'examples, Examples' will be treated differently.
  2. Semicolon (;) - All line statements must end with a semicolon character (;) (Example' var1 = 3; var2 = 8;).
  3. Blank spaces - JavaScript ignores blank spaces, tabs and blank lines that appear in the instructions, they are useful for making code more structured and easy to read. Admit only the spaces that appear in strings.
    - Example:
    var1 = 'JS tutorial';
    
    //is the same as
    var1='JS tutorial';
  4. Quotes - The single quotes ('') and double quotes (" ") are used to delimit strings. (Example: 'I learn JavaScript' or "I learn JavaScript").
  5. Special Characters - When we write scripts, sometimes we need to use a special character in values or in data output, or a key press such as the TAB key, or a new line. In that case we must use the backslash character '\' in front of one of the Escape codes:
    • \b - backspace
      \f - new page
      \n - new line
      \r - indicate a carriage return
      \t - indicate a pressing TAB key
      \\ - a backslash character
      \' - indicate an apostrophe (single quotes)
      \" - double quotes
    • - For example, if you want to display a text using document.write(), and that text should include quotes and backslash character '\', like ('JavaScript' Course \ marplo.net), to not confuse the script in code interpretation, becouse the quotes and backslash are part of the syntax, we add \ in front of these characters within the string. The command for display the string will be:
      document.write ('\' JavaScript \' Course \\ and tutorials.');
      
  6. Comments - The comments within the code are necessary when we want to specify the role and functions of certain variables or instructions, for easy understanding of the script later.
    To add a comment on a single line, inside the code, use double slash //. All characters to the right of the double slash represent a comment.
    - Example:
    // Comment on a single line
    var str ='String';
    If you want to write comments on multiple lines, use /* at the beginning of the comment, and */ at its end.
    - Example:
    /* comment on ...
    multiple lineas
     end line */
    var str ='String';
  7. The name of the variables and functions - The name given to variables and functions must follow the following rules:
    • - The first character must be a letter, an underscore (_) or $ sign.
      - The first character can not be a number.
      - the name should not contain blank space.
      - Do not use reserved words that are part of the JavaScript language (such as 'array', 'status', 'alert'), because the interpreter program will not make the difference between these names and JavaScript commands with the same name.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
JavaScript Syntax

Last accessed pages

  1. Disable button and Enable it after specified time (6744)
  2. Insert, Select and Update NULL value in MySQL (30263)
  3. XML sitemap with data from MySQL (2513)
  4. Simple PHP Upload Script (5929)
  5. PHP PDO - Select query, fetch (21001)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (559)
  2. Read Excel file data in PHP - PhpExcelReader (468)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (344)
  4. PHP getElementById and getElementsByTagName (267)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (254)