Share via


Adding JScript Code to an HTML Page

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

To add JScript code to an HTML page, enclose the script within a pair of <SCRIPT> tags, using either the TYPE attribute or the LANGUAGE attribute to specify the scripting language. You can place JScript code blocks anywhere on an HTML page. However, it is a good practice to put all general-purpose scripting code (that is, script that is not tied to a particular form control) in the HEAD section. This ensures that script will already have been read and decoded before it is called from the body of the HTML page.

Pages that rely on JScript functionality to operate, can appear broken on Web browsers that have JScript or JavaScript turned off, or on Web browsers that do not implement JScript or JavaScript functionality. For example, consider a page that relies on JScript to implement password save functionality, such as a login page for residential gateway user interface. When Internet Explorer security is set to the highest level, JScript functionality is disabled, and the Web page does not function properly.

To resolve this issue, enable JScript or JavaScript in your browser. If your browser does not support JScript or JavaScript, switch to a browser that supports these scripts. Alternately, you can lower your Internet Explorer security level.

Note

You should fully evaluate all security implications of enabling JScript or JavaScript functionality, or lowering your general Internet Explorer security settings before making this change.

The following JScript code example validates user-supplied data before sending a form.

<SCRIPT TYPE="text/JScript"> 
//Or: <SCRIPT LANGUAGE="JScript" 
function ValidateForm(eForm)
{
  // --------------------------------------------
  // Check for a blank name.
  // --------------------------------------------
  if ("" == eForm.txtName.value)
  {
    window.alert("Please enter your name.    ");
    return false;
  }
  // --------------------------------------------
  // Check for a blank password.
  // --------------------------------------------
  if ("" == eForm.txtPassword.value)
  {
    window.alert("Please enter your password.    ");
    return false;
  }
  // --------------------------------------------
  // Check for an unselected size option.
  // --------------------------------------------
  else if (0 == eForm.selSize.selectedIndex)
  {
    window.alert("Please select a size.    ");
    return false;
  }
  else
  {
  // --------------------------------------------
  // Check for no selected Crust radio buttons.
  // --------------------------------------------
    var bCrustChecked = false;
    for (var i=0;i<eForm.radCrust.length;i++)
    {
      if (eForm.radCrust[i].checked)
      {
        bCrustChecked = true;
        var sCrust = eForm.radCrust[i].value;
        break;
      }
    }
    if (!bCrustChecked)
    {
      window.alert("Please select a crust type.    ");
      return false;
    }
  }
  window.alert("All is well. Submitting form...    ");
  return true;
}
</SCRIPT>

For this script to actually prevent an invalid page from being sent, you would also need to hook up an event handler function, such as onsubmit, explicitly requesting the return value from the JScript function, as follows.

<form name="form1" onsubmit="return ValidateForm(this);">

See Also

Other Resources

JScript Application Development