Javascript Course

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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
JavaScript Syntax

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)