Tuesday, September 13, 2011

When a WebLink is not a web link

Every once in a while, a situation comes along where you want the appearance of one control, but the behavior of another. A user recently asked me if they could use a WebLink control just to execute some Realbasic code in the MouseUp event, and you can, but there's a trick!

If you add a WebLink to a page and then add a Msgbox command to the MouseUp event like this:

Sub MouseUp()
MsgBox "Hello World!"
End Sub

The problem is that the WebLink is expecting to have a URL associated with it. Leaving it blank and clicking anywhere on the text of the link causes the page to refresh, clicking outside of the text (but still on the control) will execute the code in the MouseUp event.

So how do you get the whole control to behave? In the URL field, type this:


This tells the browser that instead of going to a page, you want it to execute a command (in Javascript). Immediately following with a semicolon sends an empty command, so it does nothing, and the MouseUp event fires the way you want it to.


1 comment:

Produino said...

Thanks Greg, this is what i has as a problem. Now this is a good solution. Posting here is a good thing!