tags:

views:

66

answers:

4

I have a project where on change of select box I am loading data from the server. How do I make sure that until the data is loaded user can't do anything else on the page?

A: 

In jQuery, you can use $.ajax().

Use async: false;

But I do not recommend it.

Reigel
A: 

Put

$(this).attr('disabled', 'disabled');

into your change event handler and

$(this).removeAttr('disabled');

into your success/complete handler.

To create a blocking / invisible div do:

$('<div/>', {
    id:   'modalblock',
    css:  {
       position:   'absolute',
       top:        '0px',
       height:     '0px',
       width:      window.innerWidth,
       height:     window.innerHeight,
       zIndex:     '199',
       backgroundColor: 'transparent',
       cursor:     'wait'
    }
}).appendTo(document.body);

To remove this again use

$('#modalback').remove();

But again, this is bad karma, confuses an user like a lot because he will not expect such a behavior.

jAndy
`user cant do anything else on the page ..` so, you are disabling the page?.. ;)
Reigel
if data is loaded just when selecting anything, this should just be fine.
jAndy
@reigel yeah i need to block all interactions with user until data is loaded ...how do i do it
pradeep
what you are trying to do is pretty bad karma, which basically can be solved by using a `syncronous` ajax request. Otherwise, just display a `div` positioned absolute high z-index and the window dimensions as width/height.
jAndy
how will i know the window dimensions and all.i did a div like this <style type='text/css'>#response { border: none; padding: 50px; background-color:#000; -webkit-border-radius: 10px; -moz-border-radius: 10px; opacity: .5; color: #fff; z-index:100; position:absolute; left:30%; top: 50%; }</style>it comes at center and all . but how do i block the whole window
pradeep
check my update
jAndy
+2  A: 

You can create a modal dialog using jQuery UI or some other JavaScript library.

On the close event of the plugin you can check whether data has been received using global variable is_received. If is_received == true then let closeevent close the modal dialog by returning true else return false. I am assuming that close event stops the user from closing the modal dialog if you return false. Also you can show user friendly message inside of the modal dialog telling user that data is being loaded.

Another way could be: you disable close buttons of the modal dialog and upon receiving data from the server enable those buttons again. The modal diaog will stop the user from interacting with contents.

Ayaz Alavi
lol - that "answer has been posted" message needs to be a bit quicker
Dan Heberden
yeah a minute earlier. I think that can be only possible solution on html web pages unless you disable all input elements on webpage or hide body contents for a while
Ayaz Alavi
you can use code from this page http://jquery.malsup.com/block/#dialog . It is exactly what you want.
Ayaz Alavi
+1  A: 

From a UI perspective, loading a modal window is your best option. with jQuery, you can use jQueryui - launch a modal dialog that has a "working" or similar message to prevent user interaction.

Dan Heberden
i dont wanna use modal. until the data is loaded from server . it should show text as loading... and block the user interaction..how do i do it...
pradeep
for _everything_ or just a particular element? and if element, what kind?
Dan Heberden
oh, and maybe http://jquery.malsup.com/block/#overview will help you out
Dan Heberden
i tried it..i had googled that .. dono what was wrong. it loaded an youtube video in ma website..plugin had that video code ...
pradeep