views:

4978

answers:

4

Is it possible to set the cursor to 'wait' on the entire html page in a simple way? The idea is to show the user that something is going on while an ajax call is being completed. The code below shows a simplified version of what I tried and also demonstrate the problems I run into:

  1. if an element (#id1) has a cursor style set it will ignore the one set on body (obviously)
  2. some elements have a default cursor style (a) and will not show the wait cursor on hover
  3. the body element has a certain height depending on the content and if the page is short, the cursor will not show below the footer

The test:

<html>
    <head>
        <style type="text/css">
            #id1 {
                background-color: #06f;
                cursor: pointer;
            }

            #id2 {
                background-color: #f60;
            }
        </style>
    </head>
    <body>
        <div id="id1">cursor: pointer</div>
        <div id="id2">no cursor</div>
        <a href="#" onclick="document.body.style.cursor = 'wait'; return false">Do something</a>
    </body>
</html>

Later edit...
It worked in firefox and IE with:

div#mask { display: none; cursor: wait; z-index: 9999; 
position: absolute; top: 0; left: 0; height: 100%; 
width: 100%; background-color: #fff; opacity: 0; filter: alpha(opacity = 0);}

<a href="#" onclick="document.getElementById('mask').style.display = 'block'; return false">
Do something</a>

The problem with (or feature of) this solution is that it will prevent clicks because of the overlapping div (thanks Kibbee)

Later later edit...
A simpler solution from Dorward:

.wait, .wait * { cursor: wait !important; }

and then

<a href="#" onclick="document.body.className = 'wait'; return false">Do something</a>

This solution only shows the wait cursor but allows clicks.

+1  A: 

Why don't you just use one of those fancy loading graphics (eg: http://ajaxload.info/)? The waiting cursor is for the browser itself - so whenever it appears it has something to do with the browser and not with the page.

unexist
+8  A: 

I understand you may not have control over this, but you might instead go for a "masking" div that covers the entire body with a z-index higher than 1. The center part of the div could contain a loading message if you like.

Then, you can set the cursor to wait on the div and don't have to worry about links as they are "under" your masking div. Here's some example CSS for the "masking div":

body { height: 100%; }
div#mask { cursor: wait; z-index: 999; height: 100%; width: 100%; }
Eric Wendelin
This can often cause problems. In Firefox, puting a fixed div over the entire page causes the entire page to become unclickable. ie, you can't click on links, because you aren't clicking on the link you are clicking on the div in front of the link.
Kibbee
Works ok in firefox. No luck in IE :(. In IE it behaves exactly as if the div is not there. The only way to have the desired behavior is to set a color on the div background.
Aleris
Thanks for the observation Kibbee, that is a valid point
Aleris
Ok after a bit more playing it finally worked with: div#mask { display: none; cursor: wait; z-index: 9999; position: absolute; top: 0; left: 0; height: 100%; width: 100%; background-color: #fff; opacity: 0; filter: alpha(opacity = 0);}
Aleris
Agreed, Kibbee, it really depends on your desired behavior. It sound like Aleris does not want the user to do anything (hence the wait cursor) until AJAX calls back.
Eric Wendelin
+2  A: 

This seems to work in firefox

<style>
*{ cursor: inherit;}
body{ cursor: wait;}
</style>

The * part ensures that the cursor doesn't change when you hover over a link. Although links will still be clickable.

Kibbee
*{ cursor: wait !important; } would be simpler and more likely to work in IE (which has lots of bugs with the inherit keyword)
David Dorward
Can * { cursor: wait } be applied from javascript? - except by iterating all elements in the entire DOM :)
Aleris
maybe you could add a style element and set the innerHTML. Wouldn't be very elegant, but it might work.
Kibbee
.wait, .wait * { cusor: wait !important; } and then set document.body.className = 'wait'; with JavaScript
David Dorward
A: 

Try the css:

html.waiting {
cursor: wait;
}

It seems that if the property body is used as apposed to html it doesn't show the wait cursor over the whole page. Furthermore if you use a css class you can easily control when it actually shows it.

GameFreak