Bambang pramusintha

References to HTML elements from a script

In Tutorial, Wordpress Tutorial on July 7, 2012 at 10:50 am

References to HTML elements from a script

Each scripting language has its own conventions for referring to HTML objects from within a script. This specification does not define a standard mechanism for referring to HTML objects.

However, scripts should refer to an element according to its assigned name. Scripting engines should observe the following precedence rules when identifying an element: a

name

attribute takes precedence over an

id

if both are set. Otherwise, one or the other may be used.

18.2.3 Intrinsic events

Note. Authors of HTML documents are advised that changes are likely to occur in the realm of intrinsic events (e.g., how scripts are bound to events). Research in this realm is carried on by members of the W3C Document Object Model Working Group (see the W3C Web Site at http://www.w3.org/ for more information).

Attribute definitions

onload

= script [CT]

Theonloadevent occurs when the user agent finishes loading a window or all frames within a

FRAMESET

. This attribute may be used with

BODY

and

FRAMESET

elements.

onunload

= script [CT]

Theonunloadevent occurs when the user agent removes a document from a window or frame. This attribute may be used with

BODY

and

FRAMESET

elements.

onclick

= script [CT]

Theonclickevent occurs when the pointing device button is clicked over an element. This attribute may be used with most elements.

ondblclick

= script [CT]

Theondblclickevent occurs when the pointing device button is double clicked over an element. This attribute may be used with most elements.

onmousedown

= script [CT]

Theonmousedownevent occurs when the pointing device button is pressed over an element. This attribute may be used with most elements.

onmouseup

= script [CT]

Theonmouseupevent occurs when the pointing device button is released over an element. This attribute may be used with most elements.

onmouseover

= script [CT]

Theonmouseoverevent occurs when the pointing device is moved onto an element. This attribute may be used with most elements.

onmousemove

= script [CT]

Theonmousemoveevent occurs when the pointing device is moved while it is over an element. This attribute may be used with most elements.

onmouseout

= script [CT]

Theonmouseoutevent occurs when the pointing device is moved away from an element. This attribute may be used with most elements.

onfocus

= script [CT]

Theonfocus

event occurs when an element receives focus either by the pointing device or by tabbing navigation. This attribute may be used with the following elements:

A

,

AREA

,

LABEL

,

INPUT

,

SELECT

,

TEXTAREA

, and

BUTTON

.

onblur

= script [CT]

Theonblurevent occurs when an element loses focus either by the pointing device or by tabbing navigation. It may be used with the same elements as

onfocus.

onkeypress

= script [CT]

Theonkeypressevent occurs when a key is pressed and released over an element. This attribute may be used with most elements.

onkeydown

= script [CT]

Theonkeydownevent occurs when a key is pressed down over an element. This attribute may be used with most elements.

onkeyup

= script [CT]

Theonkeyupevent occurs when a key is released over an element. This attribute may be used with most elements.

onsubmit

= script [CT]

Theonsubmitevent occurs when a form is submitted. It only applies to the

FORM

element.

onreset

= script [CT]

Theonresetevent occurs when a form is reset. It only applies to the

FORM

element.

onselect

= script [CT]

Theonselectevent occurs when a user selects some text in a text field. This attribute may be used with the

INPUT

and

TEXTAREA

elements.

onchange

= script [CT]

Theonchangeevent occurs when a control loses the input focus and its value has been modified since gaining focus. This attribute applies to the following elements:

INPUT

,

SELECT

, and

TEXTAREA

.

It is possible to associate an action with a certain number of events that occur when a user interacts with a user agent. Each of the “intrinsic events” listed above takes a value that is a script. The script is executed whenever the event occurs for that element. The syntax of script data depends on the scripting language.

Control elements such as

INPUT

,

SELECT

,

BUTTON

,

TEXTAREA

, and

LABEL

all respond to certain intrinsic events. When these elements do not appear within a form, they may be used to augment the graphical user interface of the document.

For instance, authors may want to include press buttons in their documents that do not submit a form but still communicate with a server when they are activated.

The following examples show some possible control and user interface behavior based on intrinsic events.

In the following example, userName is a required text field. When a user attempts to leave the field, the

onblurevent calls a JavaScript function to confirm that userName has an acceptable value.

<INPUT NAME="userName" onblur="validUserName(this.value)">

Here is another JavaScript example:

<INPUT NAME="num"
    onchange="if (!checkNum(this.value, 1, 10)) 
        {this.focus();this.select();} else {thanks()}"
    VALUE="0">

Here is a VBScript example of an event handler for a text field:

    <INPUT name="edit1" size="50">    
    <SCRIPT type="text/vbscript">
      Sub edit1_changed()
        If edit1.value = "abc" Then
          button1.enabled = True
        Else
          button1.enabled = False
        End If
      End Sub
    </SCRIPT>

Here is the same example using Tcl:

    <INPUT name="edit1" size="50">
    <SCRIPT type="text/tcl">
      proc edit1_changed {} {
        if {[edit value] == abc} {
          button1 enable 1
        } else {
          button1 enable 0
        }
      }
      edit1 onChange edit1_changed
    </SCRIPT>

Here is a JavaScript example for event binding within a script. First, here’s a simple click handler:

    
<BUTTON type="button" name="mybutton" value="10">
<SCRIPT type="text/javascript">
      function my_onclick() {
         . . .
      }
    document.form.mybutton.onclick = my_onclick
 </SCRIPT>
 </BUTTON>

Here’s a more interesting window handler:

    
<SCRIPT type="text/javascript">
      function my_onload() {
         . . .
      }

      var win = window.open("some/other/URI")
      if (win) win.onload = my_onload
</SCRIPT>

In Tcl this looks like:

 <SCRIPT type="text/tcl">
     proc my_onload {} {
       . . .
     }
     set win [window open "some/other/URI"]
     if {$win != ""} {
         $win onload my_onload
     }
 </SCRIPT>

Note that “document.write” or equivalent statements in intrinsic event handlers create and write to a new document rather than modifying the current one.

18.2.4 Dynamic modification of documents

Scripts that are executed when a document is loaded may be able to modify the document’s contents dynamically. The ability to do so depends on the scripting language itself (e.g., the “document.write” statement in the HTML object model supported by some vendors).

The dynamic modification of a document may be modeled as follows:

  1. All
    SCRIPT
    elements are evaluated in order as the document is loaded.
  2. All script constructs within a given
    SCRIPT
    element that generate SGML CDATA are evaluated. Their combined generated text is inserted in the document in place of the

    SCRIPT

    element.

  3. The generated CDATA is re-evaluated.

HTML documents are constrained to conform to the HTML DTD both before and after processing any

SCRIPT

elements.

The following example illustrates how scripts may modify a document dynamically. The following script:

 <TITLE>Test Document</TITLE>
 <SCRIPT type="text/javascript">
     document.write("<p><b>Hello World!<\/b>")
 </SCRIPT>

Has the same effect as this HTML markup:

<TITLE>Test Document</TITLE>
 <P><B>Hello World!</B>

References to HTML elements from a script

References to HTML elements from a script

Each scripting language has its own conventions for referring to HTML objects from within a script. This specification does not define a standard mechanism for referring to HTML objects.

However, scripts should refer to an element according to its assigned name. Scripting engines should observe the following precedence rules when identifying an element: a

name

attribute takes precedence over an

id

if both are set. Otherwise, one or the other may be used.

18.2.3 Intrinsic events

Note. Authors of HTML documents are advised that changes are likely to occur in the realm of intrinsic events (e.g., how scripts are bound to events). Research in this realm is carried on by members of the W3C Document Object Model Working Group (see the W3C Web Site at http://www.w3.org/ for more information).

Attribute definitions

onload

= script [CT]

Theonloadevent occurs when the user agent finishes loading a window or all frames within a

FRAMESET

. This attribute may be used with

BODY

and

FRAMESET

elements.

onunload

= script [CT]

Theonunloadevent occurs when the user agent removes a document from a window or frame. This attribute may be used with

BODY

and

FRAMESET

elements.

onclick

= script [CT]

Theonclickevent occurs when the pointing device button is clicked over an element. This attribute may be used with most elements.

ondblclick

= script [CT]

Theondblclickevent occurs when the pointing device button is double clicked over an element. This attribute may be used with most elements.

onmousedown

= script [CT]

Theonmousedownevent occurs when the pointing device button is pressed over an element. This attribute may be used with most elements.

onmouseup

= script [CT]

Theonmouseupevent occurs when the pointing device button is released over an element. This attribute may be used with most elements.

onmouseover

= script [CT]

Theonmouseoverevent occurs when the pointing device is moved onto an element. This attribute may be used with most elements.

onmousemove

= script [CT]

Theonmousemoveevent occurs when the pointing device is moved while it is over an element. This attribute may be used with most elements.

onmouseout

= script [CT]

Theonmouseoutevent occurs when the pointing device is moved away from an element. This attribute may be used with most elements.

onfocus

= script [CT]

Theonfocus

event occurs when an element receives focus either by the pointing device or by tabbing navigation. This attribute may be used with the following elements:

A

,

AREA

,

LABEL

,

INPUT

,

SELECT

,

TEXTAREA

, and

BUTTON

.

onblur

= script [CT]

Theonblurevent occurs when an element loses focus either by the pointing device or by tabbing navigation. It may be used with the same elements as

onfocus.

onkeypress

= script [CT]

Theonkeypressevent occurs when a key is pressed and released over an element. This attribute may be used with most elements.

onkeydown

= script [CT]

Theonkeydownevent occurs when a key is pressed down over an element. This attribute may be used with most elements.

onkeyup

= script [CT]

Theonkeyupevent occurs when a key is released over an element. This attribute may be used with most elements.

onsubmit

= script [CT]

Theonsubmitevent occurs when a form is submitted. It only applies to the

FORM

element.

onreset

= script [CT]

Theonresetevent occurs when a form is reset. It only applies to the

FORM

element.

onselect

= script [CT]

Theonselectevent occurs when a user selects some text in a text field. This attribute may be used with the

INPUT

and

TEXTAREA

elements.

onchange

= script [CT]

Theonchangeevent occurs when a control loses the input focus and its value has been modified since gaining focus. This attribute applies to the following elements:

INPUT

,

SELECT

, and

TEXTAREA

.

It is possible to associate an action with a certain number of events that occur when a user interacts with a user agent. Each of the “intrinsic events” listed above takes a value that is a script. The script is executed whenever the event occurs for that element. The syntax of script data depends on the scripting language.

Control elements such as

INPUT

,

SELECT

,

BUTTON

,

TEXTAREA

, and

LABEL

all respond to certain intrinsic events. When these elements do not appear within a form, they may be used to augment the graphical user interface of the document.

For instance, authors may want to include press buttons in their documents that do not submit a form but still communicate with a server when they are activated.

The following examples show some possible control and user interface behavior based on intrinsic events.

In the following example, userName is a required text field. When a user attempts to leave the field, the

onblurevent calls a JavaScript function to confirm that userName has an acceptable value.

<INPUT NAME="userName" onblur="validUserName(this.value)">

Here is another JavaScript example:

<INPUT NAME="num"
    onchange="if (!checkNum(this.value, 1, 10)) 
        {this.focus();this.select();} else {thanks()}"
    VALUE="0">

Here is a VBScript example of an event handler for a text field:

    <INPUT name="edit1" size="50">    
    <SCRIPT type="text/vbscript">
      Sub edit1_changed()
        If edit1.value = "abc" Then
          button1.enabled = True
        Else
          button1.enabled = False
        End If
      End Sub
    </SCRIPT>

Here is the same example using Tcl:

    <INPUT name="edit1" size="50">
    <SCRIPT type="text/tcl">
      proc edit1_changed {} {
        if {[edit value] == abc} {
          button1 enable 1
        } else {
          button1 enable 0
        }
      }
      edit1 onChange edit1_changed
    </SCRIPT>

Here is a JavaScript example for event binding within a script. First, here’s a simple click handler:

    
<BUTTON type="button" name="mybutton" value="10">
<SCRIPT type="text/javascript">
      function my_onclick() {
         . . .
      }
    document.form.mybutton.onclick = my_onclick
 </SCRIPT>
 </BUTTON>

Here’s a more interesting window handler:

    
<SCRIPT type="text/javascript">
      function my_onload() {
         . . .
      }      var win = window.open("some/other/URI")
      if (win) win.onload = my_onload
</SCRIPT>

In Tcl this looks like:

 <SCRIPT type="text/tcl">
     proc my_onload {} {
       . . .
     }
     set win [window open "some/other/URI"]
     if {$win != ""} {
         $win onload my_onload
     }
 </SCRIPT>

Note that “document.write” or equivalent statements in intrinsic event handlers create and write to a new document rather than modifying the current one.

18.2.4 Dynamic modification of documents

Scripts that are executed when a document is loaded may be able to modify the document’s contents dynamically. The ability to do so depends on the scripting language itself (e.g., the “document.write” statement in the HTML object model supported by some vendors).

The dynamic modification of a document may be modeled as follows:

  1. All
    SCRIPT
    elements are evaluated in order as the document is loaded.
  2. All script constructs within a given
    SCRIPT
    element that generate SGML CDATA are evaluated. Their combined generated text is inserted in the document in place of the

    SCRIPT

    element.

  3. The generated CDATA is re-evaluated.

HTML documents are constrained to conform to the HTML DTD both before and after processing any

SCRIPT

elements.

The following example illustrates how scripts may modify a document dynamically. The following script:

 <TITLE>Test Document</TITLE>
 <SCRIPT type="text/javascript">
     document.write("<p><b>Hello World!<\/b>")
 </SCRIPT>

Has the same effect as this HTML markup:

 <TITLE>Test Document</TITLE>
 <P><B>Hello World!</B>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: