From WikiEducator
Jump to: navigation, search



VmvIcon Objectives.png

By the end of this page you will be able to:

  • Understand how JavaScript is placed in an HTML document
  • Understand how Browsers handle JavaScript

Code placement

  • JavaScript can be inserted into the HEAD or BODY section of a document.
  • If code is to be reused within a page then it is better to place the code in the HEAD section.
  • Code in the HEAD section is loaded before the page is built and any referrals within a page are already in memory.
  • Code within the page must be loaded into memory before execution.

Browser Handling

  • Older style browsers may not handle JavaScript
  • Browsers such as Internet Explorer warn you that running scripts from your local hard drive is a security risk
JavaScript security warning in Internet Explorer
  • To trick the browser we enclose the JavaScript code between a <!-- and //-->
<script type="text/javascript">
<!--     JavaScript code  
  • The older browser then ignores all code between the start and end tags
  • You may come across JavaScripts inserted as
  <script language="JavaScript"> 

This has now be deprecated and the following format should be used:

  <script type="text/javascript">

JavaScript (js1_02)

  <script type="text/javascript">
    <!-- hide from old browsers     
    document.write("<p>This is code in the HEAD!!!</p>"); 
    // -->  
  <p>This is a normal HTML code in the BODY section.<br />  
  <script type="text/javascript">
    <!-- hide from old browsers     
    document.write("This is JavaScript in the BODY!!!");  
    // -->  
  <br />
  Back to normal HTML again.</p>

Example :Click here to run js1_02.

JavaScript: Order of execution

VmvIcon References.png References

virtualMV  |  Superquick wiki guide  |  Please give me some feedback

VirtualMV/JavaScript/Starting/Basics. (2022). In WikiEducator/VirtualMV wiki. Retrieved August 16, 2022, from http:    (zotero)