Genii Weblog

Rendering JavaScript actions on the web

Thu 18 Dec 2003, 12:29 PM

by Ben Langhinrichs
As so often happens to me, I was answering a question (on the ND6 Gold Forum) when my answer triggered a question for me.  If you look at my answer, you will see two action hotspots.  One uses formula language and has the formula:

@URLOpen("JavaScript:alert(\'Hi!  I am a JavaScript alert called through URLOpen\')")

while the other is a JavaScript action and has the script:

alert('Hi!  I am a JavaScript alert called directly')

I would have expected that the HTML generated would be approximately the same, but for the change in the message, but here is what Domino generates for the @URLOpen call:

<a href="JavaScript:alert('Hi!  I am a JavaScript alert called through URLOpen')"><u><font color="#008000">URLOpen action</font></u></a>

and here is what it generates for the JavaScript action:

<a onclick="alert('Hi!  I am a JavaScript alert called directly')
return false;" href=""><u><font color="#008000">JavaScript action</font></u></a>

The first uses the href to contain the JavaScript, while the second uses the onclick event and sets the href to "".  

Now, having seen the results, I understand somewhat why the two would be generated differently.  What I am less clear about are the implications.  Which is "better"?  Here are a few observations which make me unsure (all observations made from IE 5.5, because that is what I am using):
  • When I mouse over the @URLOpen action, the status bar at the bottom says "Shortcut to javascript:alert('Hi!  I am a JavaScript alert called through URLOpen')", but when I mouse over the JavaScript action, the status bar says "Shortcut to 55c38d716d632d9b8525689b005ba1c0", which is really useless because it is just the view unid.
  • When I right click on the action hotspot and choose Open in New Window, which people might well do with what appears to be a link, @URLOpen opens a window, puts the "javascript:alert('Hi!  I am a JavaScript alert called through URLOpen')" in the address bar, but then actually executes the action.  Not terribly useful, but better than nothing.  When I do the same with the JavaScript action, it puts the words "about:blank" in the address bar, and doesn't execute the action.
  • While it is a minor point, the JavaScript action takes 14 characters more, even accounting for the different message and all.

So, what I want to know is, which is better?  I would have assumed the JavaScript action was better, and it does have advantages for longer scripts where the JavaScript syntax checking and such are valuable, but the considerations above make me wonder.  Are there other advantages to one or the other that I have not considered?  Are there different implications on different browsers?  Should I stop worrying about this sort of picayune detail and get real work done?  Any thoughts?

Copyright 2003 Genii Software Ltd.

What has been said:

84.1. Stan Rogers
(12/18/2003 01:00 PM)

The "javascript:" pseudo-URL was originally a workaround for a lack of ubiquitous onclick events. There may still be a few really old browsers that don't recognize an onclick from a link tag, but you should be okay in gen4 and up using either. That's the "machanics". The philosophy is another matter entirely. There are two camps -- those that believe that href means "hypertext refer" and nothing else, and those that believe that clicking on anything clickable should have one and only one codepath. If you're in the first camp, you abhor "javascript:" simply because it is not, in fact, a URL. If you're in the second, you'd choke at the mere thought that the link should specify both a jumpto location (the href) AND something else to do when the link is clicked (the onclick).

Which is "right"? Well, is ketchup an acceptable condiment for use on a hot dog? Bring that up in a public discussion, and you'll get the same kind of religious argument. Personally, I prefer pseudoURLs and mustard+onions. But that is just my opinion.

84.2. Stan Rogers
(12/18/2003 01:01 PM)

When I grow up, I want to learn how to spell "mechanics", among other things.

84.3. Ben Langhinrichs
(12/18/2003 01:30 PM)

Out of curiosity, when you create an action hotspot with JavaScript, do you use the @URLOpen action technique? Or do you simply prefer it?

84.4. Stan Rogers
(12/18/2003 01:41 PM)

I tend to use the JavaScript version (as opposed to the URL version) if I'm using the Notes-native method, but that has more to do with avoiding all of the backslashing and quoting and so forth. Why anyone would choose to write JavaScript in Formula Language if there is an alternative is beyond me -- there's just too much than can go wrong, even in the shortest bit of code.

In my own HTML, though, I tend to use the pseudoURL. I save the onclick for buttons, input images, and other things that only do what they do from the onclick.

84.5. Richard Schwartz
(12/30/2003 01:45 PM)

I have a suspicion that behavior of the href version may vary depending on whether or not the target (explicit or default) is outside of the current window, whereas the onClick version should always execute in the current window's context.