Senin, 22 Desember 2025

Script Built-In

 

Naming Conventions

The final Internet Explorer-specific event-hookup mechanism consists of naming your function to include the object and eventname. As long as your function is name objectname.eventname, IE will hook up the function automatically. For example:

<span id="testnaming">Click me naming - JScript</span>
<script language=JScript>

function testnaming.onclick()
{
   alert("script naming - JScript")
}
</script>

Note:   This mechanism only works with JScript.

Automagic

In addition to all the IE-specific event handling mechanisms, you can also use the built-in "automagic" hook up provided by the script engines. The key to using this mechanism is understanding when the hook up occurs. If the objects or HTML elements are available when the page loads, then the script engine will do the event binding. If, however, the object or elements are added after page load (by setting innerHTML for example) then the script engine won't hook up the events. The good thing about this mechanism is you can use it to respond to ActiveX Control events.

<span id="testscriptbuiltin">Click me Built In - JScript</span>
<script language=JScript>

function testscriptbuiltin::onclick()
{
alert("script naming - JScript")
}
</script>

<br>

<span id="testscriptbuiltin">Click me Built In - VBScript</span>
<script language=VBScript>

sub testscriptbuiltin_onclick()
   alert("script naming - VBScript")
end sub
</script>


============================================

Function Pointers

Every function in JScript is a first-class object, which allows you to pass around pointers to the function and use them to call the function. This offers flexibility in hooking up event handlers, enabling you to use the same function to handle one or many events. To setup function-pointer events, just set the EventName property on the object to the pointer of the function. In JScript this is very simple to do. For example:

window.onclick=foo
function foo()
{
   alert('you clicked on the window')
}

In the first releases of VBScript, there was no way to use function pointers, since if you set window.onclick to foo, VBScript would call foo rather than get a pointer. In VBScript 5.0 the GetRef method was introduced. This created a reference to the function, allowing you to set the event handler. For example:

set window.onclick=GetRef("foo")
function foo()
   alert('you clicked on the window')
end function

<Script> Block-Event Handlers

The event handlers used so far work well for HTML events, but don't provide a way for writing event handlers for custom events on ActiveX objects that you may have in your page. To provide this support, Internet Explorer introduced extensions to the <script> block to specify which object and which event the script is for. For example:

<span id="testscript">Click me script block handler - JScript</span>
<script language=JScript for=testscript event="onclick">
alert("script block handler - JScript")
</script>
<br>

======================================

Tidak ada komentar: