Binding events to objects

Jake Howlett, 30 October 2000

Category: JavaScript; Keywords: script event function

In domino it is almost impossible to have complete control of the resulting HTML when designing your pages. It is only through the use of trickery that it is possible to achieve most goals. One such occasion would be controlling the "events" of the "form" element on a document. Say you wanted to have some JavaScript execute in the onLoad event of the, domino-generated, form. In R5 this is easy as all the form's events are made available in the designer client. However, in R4, this is not possible.

In Internet Explorer 4+, the event model will allow binding of object events to a "script" element. When the page loads the script elements are associated with the event of the object. Place the script elements in the page's header with the following syntax:

<script for=firstNameEntry event="onload()">
...
</script>


firstNameEntry is the unique ID attribute of the element whose event you wish to handle. For example, if you have a form called "Repository", with an alias of "rep", domino will call the form "_rep". So, the html needed to tell its onSubmit event what to do would be:

<script for=_rep event="onsubmit()">
return (document._rep.aField.value == "") ? false : true;
</script>


This will then stop the form being submitted until the field called "aField" has been filled out.

The same principle can be applied to almost all elements that have events associated with them. You can also use reserved words in the "for" attribute such as "window".

<script for=window event="onload()">
alert('You have opened this page');
</script>


Example: Here is a button that we know is called "btnTest" but whose HTML we can alter:

<input type="button" value="Test" name="btnTest" />



This script below (it MUST appear after the object so that event can be binded properly) applies to its onclick() event and calls an alert box:

<script for=btnTest event="onclick()">
alert('If you see this without pressing a button then you must be using Netscape.');
</script>


Try pressing it if you don't believe me.

Note: Netscape Navigator will try and execute this code as the page loads. It is important to be sure of your target browser when scripting like this.