Search

Top Ten List

Of all my ramblings, people seem to find the following the most interesting:
  1. Pimp My Fields
  2. Using the WebBrowser control to view attachments inline
  3. Purty charts in Domino
  4. My approach to DbLookup and DbColumn in Javascript
  5. Mind Map of Database Design
  6. Design Catalog - version control for Domino
  7. Workaround for LotusScript event binding
  8. Clickable URL's in Notes view columns
  9. Every time you use window.open, God kills a kitten
  10. Create, edit, and delete without agents via AJAX

« Fun with Pass-Thru HTML | Main| Good ol' Let »

Example of using AJAX in the Notes client

Category domino
At Ben's request, here's a very basic example of how I've put AJAX to use within the Notes client: a ZIP code lookup.

Suppose you have a form that asks the author to populate address information. To save them a few keystrokes, you can make a quick AJAX call to obtain the city and state and populate them automatically. This is easier to implement than you might expect:

1. Paste the following into the JS Header section of the form (or include in a JavaScript library inserted into the JS Header as a resource):

var gstrCity;
var gstrState;
var gvarXMLApp;

function getCityState(pstrZipCode) {
    var strServiceURL;
    var gvarXMLApp;
    loadXMLDoc();
    strServiceURL = "http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=" + pstrZipCode;
    gvarXMLApp.open("GET", strServiceURL, true);
    gstrCity = gvarXMLApp.getElementsByTagName("CITY");
    gstrState = gvarXMLApp.getElementsByTagName("STATE");
}

function loadXMLDoc() {
    gvarXMLApp = false;
// branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
            try {
            gvarXMLApp = new XMLHttpRequest();
        } catch(e) {
            gvarXMLApp = false;
        }
// branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
            try {
                    gvarXMLApp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                gvarXMLApp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                gvarXMLApp = false;
            }
        }
    }
}

2. Paste the following into the onBlur event of the ZIP code field:

var strZIPCode = document.forms[0].elements("ZipCode").value;
if (strZIPCode != "") {
     getCityState(strZIPCode);
     document.forms[0].elements("City").value = gstrCity;
     document.forms[0].elements("State").value = gstrState;
}

3. Replace the element names above with the actual field names that you're using on the form.

Comments

Gravatar Image1 - Very cool. I am still trying to find a way I can squeeze this into the Lotusphere Session database. Thanks! (I just realized that I read this before but never commented - very rude)

Gravatar Image2 - That reminds me... I still need to submit a purchase req so I can get booked to go to Lotusphere. Running out of time...

Gravatar Image3 - Ram, although LotusScript and Java both support a getDocumentById method (as a member of the NotesDatabase class in LotusScript, the Database class in Java), there is no direct prewritten equivalent in JavaScript. I've also just confirmed that the Notes client (as recent as 7.0.1) does not support document.getElementById(). The only way to obtain a handle on a field using JavaScript in the client is the document.forms[0].elements() array. On the bright side, it does treat the array like a LotusScript List in addition to being an array: you can loop through the elements, but you can also refer to them by name, as described above.

Gravatar Image4 - hi Tim,
I have a question on notes client . Will notes supports getDocumentById method?

Thanks
Ram

Contact Me

Hire Me

Elsewhere

What the Quote?

"Peel me a grape, boil me some ice, split me a pea, save the pod for my pancakes"

Laura Tripcony

"I was talking to your boss last night, and he told me, 'I have some really bright people... I'm just not so sure that they're sane'."

Mary Beth Raven

"Doublemint me that."

Steven Rodgers

"Fuzzy like peach, not furry like chimp"

Tim Tripcony

"Papa Felpie's? That's an odd name for a restaurant."

Brent Bowers

Apparel

Lotus Rocks

I write the code that makes the young girls cry

Current Terror Alert Level

Assorted Linkage

ClustrMap