July 10, 2012

Change browser cursor with Prototype.js

Question by xain

I’m having problem changing the cursor when the page is loaded; my code is:

Event.observe(window, 'load', function() {
    document.body.style.cursor = 'wait';
     $$('select').each(function(s) {
        var ajaxRequest = new Ajax.Request(
            '/some_ajax_proc',
            {

When the page loads, the cursor doesn’t change. However it does with no problem in other event listeners further on such as:

   $('mytxt').observe('change', function() {
      document.body.style.cursor = 'wait';
   }

UPDATE: OK, it DOES change the cursor, but since there’s a document.body.style.cursor = 'default'; after the Ajax loop, it changes it back immediatly so I guess it is a threads issue. Any hints in this case ?

Answer by Nullpo

Have you tried this?

Ajax.Responders.register({
   onCreate: function() {
                if (Ajax.activeRequestCount === 1) {
                   //Code for change the cursor, something like this:
                   document.body.style.cursor = 'wait';
                }
             },
   onComplete: function() {
                  if (Ajax.activeRequestCount === 0) {
                     // End loading...
                     ....
                  }
               }
});

Answer by Starx

Since, you are already using jquery.

$('mytxt').observe('change', function() {
   $("body").css("cursor", "wait");
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!