Examples: JavaScript object model

  1. This JSHeader event handler declares a function that writes the product of the Width and Length fields to the SquareFeet field.
    function getSquareFeet() {
        with (window.document.forms[0]) {
            SquareFeet.value = Width.value * Length.value
        }
    }

    This onFocus event handler for both the Length and Width fields calls the function:

    getSquareFeet()
  2. This Domino form onLoad event handler sets the window status display and then displays browser and platform information.
    window.status = "Domino window opened"
    n = navigator
    alert (n.appName + " " + n.appVersion + " on " + n.platform)
  3. This Domino hotspot onClick event handler loads a new URL by writing to the href property of the location object.
    location.href = "http://localhost/Web+test.nsf/Main+View"
  4. These onLoad event handlers are for two pages in a frameset. When the focus is on the first frame, the status bar displays "Page one." When the focus is on the second frame, the status bar displays "Page two."
    window.status = "Page one"
    window.status = "Page two"
  5. This onClick event handler for a page action displays the names of all the frames in a non-nested frameset.
    for (n = 0; n < top.length; n++) {
        alert(top.frames[n].name)
    }
  6. This onClick event handler for a page action displays the names of all the frames in a frameset that may contain one level of nesting.
    for (n = 0; n < top.length; n++) {
        w = top.frames[n]
        if (w.length == 0) {
            alert (w.name)
        }
        else {
            for (nn = 0; nn < w.length; nn++) {
                ww = w.frames[nn]
                alert (w.name + ": " + ww.name)
            }
        }
    }
  7. This onClick event handler for a page action displays the names of the applets on the page.
    for (n = 0; n < window.document.applets.length; n++) {
        alert (window.document.applets[n].name)
    }
  8. This code, placed in "Other" under the <HTML> tag of the HotSpot Properties box for a link hotspot, queries the user for a confirmation before going to the link.
    onClick="return confirm('Go?')"
  9. This code is for the onClick event handler of a picture named ThePicture. When the user clicks the picture, it changes to the image resource in "Web Test.nsf" named newdam.gif.
    document.ThePicture.src = "/Web+Test.nsf/newdam.gif"
  10. This onClick event handler for a button displays information about all the elements of the elements array in the current JavaScript form.
    e = window.document.forms[0].elements
    for (n = 0; n < e.length; n++) {
        if (e[n].name == "") ee = "NO NAME"
        else ee = e[n].name
        t = e[n].type
        if (e[n].value == "") v = "NO VALUE"
        else v = e[n].value
        alert (ee + "\n" + t + "\n" + v)
    }
  11. This onBlur event handler for FieldTwo forces FieldTwo to be blank if FieldOne contains the value "NONE."
    one = window.document.forms[0].FieldOne.value
    two = window.document.forms[0].FieldTwo.value
    if (one == "NONE" && two != "") {
        window.document.forms[0].FieldTwo.value = ""
        alert ("FieldTwo must be blank if FieldOne is NONE")
    }
  12. This onClick event handler for a button displays the selected values of a "Dialog list" field.
    with (window.document.forms[0].MyList) {
        for (n=0; n<length; n++) {
            if (options[n].selected) {
                alert (options[n].text)
            }
        }
    }
  13. This onClick event handler for a button displays all the hidden fields in the document. This only works in browsers and only if Generate HTML for all fields is selected in the Domino Forms Properties box.
    with (window.document.forms[0]) {
        for (n=0; n<elements.length; n++) {
            if (elements[n].type == "hidden") {
                alert (elements[n].name + "\n" + elements[n].value)
            }
        }
    }