SCRIPT object (Object/HTML)

An object that represents a <SCRIPT> block within the document.

Availability:

DOM level - 1
JavaScript - 1.5
JScript - 3.0
Internet Explorer - 4.0
Netscape - 6.0
Inherits from:Element object
JavaScript syntax:IEmySCRIPT = myDocument.all.anElementID
IEmySCRIPT = myDocument.all.tags("SCRIPT")[anIndex]
IEmySCRIPT = myDocument.all[aName]
-mySCRIPT = myDocument.getElementById(anElementID)
-mySCRIPT = myDocument.getElementsByName(aName)[anIndex]
-mySCRIPT = myDocument.getElementsByTagName("SCRIPT")[anIndex]
HTML syntax:<SCRIPT> ... </SCRIPT>
Argument list:anIndexA reference to an element in a collection
aNameAn associative array reference
anElementIDThe ID value of an Element object
Object properties:charset, defer, event, htmlFor, readyState, recordNumber, src, text, type
Event handlers:onClick, onDblClick, onError, onHelp, onKeyDown, onKeyPress, onKeyUp, onLoad, onMouseDown, onMouseMove, onMouseOut, onMouseOver, onMouseUp, onReadyStateChange

Given that you can access a SCRIPT object, you may be tempted to write some self-modifying code. This is not recommended. You should be able to accomplish everything you need to do in that respect with an eval() method and that would be more widely supported across browsers.

Accessing the script block may be useful to ascertain whether a particular function is available, although you should know that since you wrote the page yourself. On the other hand, if you imported a script block with a reference to a .js file, you may not know the provenance of its contents.

In the example, the source text is extracted from a script block and executed with an eval() function. The variable value is set according to the evaluated script which replaces the default value.

Warnings:

Example code:

   <HTML>
   <HEAD></HEAD>
   <BODY>
   <SCRIPT ID="ONE">
   block = "ONE";
   </SCRIPT>
   <SCRIPT ID="TWO">
   block = "TWO";
   </SCRIPT>
   <SCRIPT ID="THREE">
   block = "THREE";
   </SCRIPT>
   <SCRIPT ID="FOUR">
   block = "FOUR";
   mySourceText = eval(document.scripts.THREE.text);
   
   document.write(block);
   </SCRIPT>
   </BODY>
   </HTML>

See also:Document.scripts[], Element object

PropertyJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
charset1.5 1.5 n/a6.0 6.0 n/a n/a n/a n/a1 1 n/a n/aWarning
defer1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning
event1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning , ReadOnly.
htmlFor1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning , ReadOnly.
readyState n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly.
recordNumber n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aWarning , ReadOnly.
src1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning , ReadOnly.
text1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning , ReadOnly.
type1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a1 1 n/a n/aWarning , ReadOnly.

Event nameJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
onClick1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onDblClick1.51.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onError1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
onHelp n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aWarning
onKeyDown1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onKeyPress1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onKeyUp1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onLoad1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a n/aWarning
onMouseDown1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onMouseMove1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onMouseOut1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onMouseOver1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onMouseUp1.5 1.53.0 3.06.0 6.04.0 4.0 n/a n/a n/a n/a n/a4.0 4.0Warning
onReadyStateChange n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-

Inheritance chain:

Element object, Node object