Javascript - Cursors

Javascript provides a number of predefined cursors. For a somewhat cumbersome-to-use list, see CSS cursor Property and the related live examples.

This page splits those cursors into 3 groups - resize, custom, and everything else - and allows you to see the cursor by simply moving the cursor over the various values. This page is just an attempt to make them easier to see.


Resize


Custom

This allows you to define any cursor you want - This example uses the graphics (images) from the w3schools live example. cur-files allow the definition of a hotspot - gif- and png-files do not.


Everything else

Many of these are browser dependent.


The *wait* cursor

Setting (and using) most of the cursors is fairly straight forward. However the wait and progress cursors are a bit tricky. Basically, when these are set via code, they will not be displayed until after the code has completed executing!!! This kinda defeats the purpose.

To be clear, in one of my applications, I have code that requires about 30 seconds to complete. As a result, I want to inform the user that the last click was actually recognize and that there will be a wait.

The problem is that javascript is single threaded - meaning that cursor changes will not be displayed until the current thread - that's right, the thread that sets the cursor and runs the slow code - terminates. Thus, it is easy to change the cursor after the slow code runs .. but not before. (I have spent over a week on this.)


Timers

The idea here is to change the cursor, set a timer to start the slow code, and then terminate the original event so that the cursor will actually change before the code is run.

Apparently, the timer runs in the main (only) thread and, therefore, does not fix the problem.

On the other hand, this technique did allow the checkbox to show the change before running the slow code - it just didn't allow the cursor to change.

It is possible that I missed something and that this technique can be made to work. This code example sort of works - but there are a number of scenarios that need to be investigated before claiming success. What I tried was almost identical (I think) - good luck.


Using onMouseDown

One suggested solution (ref) is to set the document cursor on a mouse down event and to start the code on mouse up. Tried that - it did not work.

However, the following does work. (I am using any of several checkboxes to trigger the slow code.) I frequently place checkboxes inside labels (so that clicking the label is the same as clicking the checkbox). However, onMouseDown must be attached to both the checkbox and the label. And, of course, they must both be cleared when the long operation completes.

In my application, onChange is attached to the enclosing table, and not to each checkbox. Adding onMouseDown to the table does not work - it must be attached to each checkbox. (I also tried using onMouseUp attached to the table .. but that also didn't work.)

Using onMouseDown has a bad side effect - if the user presses the mouse button, and then moves off the checkbox before releasing the button, then

Rather than explicitly pass *this* in the function call, I tried using document.activeElement, but that does not work


Using a separate div layer

I tried placing a shield over the entire page. The hign z-index works when I set it manually via the debugger. but has no effect when setting it in the onChange event .. same result as before when just setting the cursor. mcUIWindowBackground sets the size and location of the shield.

Basically, javascript is a single threaded language and certain actions will have no effect until the code thread terminates. This is why the onMouseDown and onMouseUp events must be used - so that the first thread will terminate and the new cursor will be displayed.

Unfortunately, using the shield also means that the mouse up and mouse clicked events will not be seen by the underlying components. Attaching onMouseUp to the shield works, but if the mouse is moved off the checkbox while it is down, it will still be clicked - marginal at best. Of course it may be possible to add even more code to make sure that the mouse is still over the same checkbox - but not today. (I've had enough of this crap!)

Attaching the shield via code was a bit tricky. At first, I tried the following - but it caused multiple problems with the application.

This technique (which is almost identical) worked. I also tested onClick, it does not work - must use onMouseDown.

In one version, I set the cursor in the code - not the style definition. This was because the shield is slightly larger than the page and that results in the wait cursor being visible in areas where it should be the normal arrow. The following code was called on onMouseDown.

After that was working, I decided to add 30% black to the shield to indicate that nothing on the page could be clicked. Unfortunately, just like the cursor, using just the z-index caused the 30% black to show around the other page elements. As a result, the final code uses display to control both the color and cursor.


Yet another Failed method

I also tried this technique from w3schools - it also failed.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Javascript / Cursors.html