views:

38

answers:

3

So what I trying to do is to change the cursor to wait when some page is loading.

I thought this was possible with css, I trying to achieve this when someone click on some link, so what I have is this:

#something a:hover { cursor: hand; }
#something a:active { cursor: wait; }

But this doesn't work, it's a hand when hover links, and when for a second is wait, but I want this wait to continue until the new page appear.

So my question is: Is this wrong? To achieve what I want?
Or do I have to use javascript?

A: 

The meaning of the cursor property in relation to CSS selector is that when the mouse is over an element matching the selector then use the cursor. So to change the cursor for the overall document you need to do something like:

html {
    cursor: wait;
}

Obviously, this will change the cursor forever (or until the user closes the page, whichever comes first). To do this dynamically you need to use javascript:

document.body.style.cursor = 'wait';

Note that cursor:hand is only supported by IE and only needed for IE 5. The correct cursor name is pointer. Of course, if you need to support IE 5 you need to use cursor:hand. Instead of using browser sniffing you can use class name to change cursor:

CSS:

.handCursor {
    cursor: pointer;
    cursor: hand;
}

JS:

document.body.className = 'handCursor';
slebetman
I had also tried something like this, but didn't work. Certainly I've made something wrong, I guess applying it to the element.How do I apply this inside the link, inside html?
Pat
+1  A: 

The way to do this is something like this:

On the first page (to show as soon as the link is clicked):

<a href="http://www.example.com/Page2.html" onclick="document.body.style.cursor='wait'; return true;">

On the second page (to show until the new page finishes loading):

<script type="text/javsacript">
    //Set the cursor ASAP to "Wait"
    document.body.style.cursor='wait';

    //When the window has finished loading, set it back to default...
    window.onload=function(){document.body.style.cursor='default';}
</script>
Basiclife
I had also tried something like this, but didn't work. Certainly I've made something wrong, I guess applying it to the element. How do I apply this inside the link, inside html?
Pat
Ok, now I understand - you've got a 2-step process - 1) in the links onclick() event, you need to set the cursor to wait for the document body. 2) on the NEW page, you need to set the cursor to wait untilt the page finishes loading (as in my answer above). No matter what you do, there will be a little time between the old page unloading and the new page loading when you have no control over the cursor...
Basiclife
That was my problem! It didn't put anything on the new page, although it make totally sense! Actually I wasn't really understanding but like that make sense. Thanks a lot! :)
Pat
Glad I could help. Be aware that in some browsers, JavaScript stops being executed once a new page is being loaded - so make sure you always set the cursor before trying to change the page (as demo'd above) rather than after. Good luck
Basiclife
I would like to change the cursor to some image. Do you known any way how to do this? Or some nice place where I could learn jquery or javascript?
Pat
You can't use an arbitrary image as the cursor - what you;d need to do is create an img tag and constantly move it to the location of the mouse - JQuery tutorial here: http://docs.jquery.com/Tutorials I'd recommend you learn JavaScript before you touch JQuery - JQuery is an extension of JS but if you don't understand the fundamentals, you won't know "why" when something unexpected happens... Just google "Javascript tutorial"
Basiclife
A: 

As 'answered' says you can use CSS to attach the cursor to the html element, which should cover the whole page.

But you'll need to add a listener to every single anchor in the page with an onclick, which calls a function that sets the cursor on the HTML or body element. When the page reloads the cursor will go back to default again as trhe javascript would've refreshed

var anchors = document.getElementsByTagName("a");
for(var i=0,len=anchors.length;i<len;i++)
{

anchors[i].onclick = function()
{

document.body.style.cursor = "wait";

};

}
Alex
well I'm not sure why but I'm not able to make any of these examples work
Pat
Do you have an example page we could look at?
Alex
Well I tried your example. Then in the html template, I've a table, inside <td> I've got the link: <td><a href="#">lala</a></td> and your example. In the css when a:hover, cursor is pointer. and for the rest is used your example..Perhabs is something silly, Javascript is still weird for me :P
Pat