Friday, September 19, 2014

JQUERY - To avoid skip the JS methods and process seqeuntially

In some cases the js sub function or load content will be skipped due to async behaviour of running script.

The following sample will address those problems. The script  show the alert message. See the first message comes inbetween the ajaxsetup and another is outside the ajaxsetup.

This is helpful to do the work sequentially with out skip.

Step 1:
 call the jquery CDN link or give your local downloaded path of jquery file in the following script tag: Here i used CDN link

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>


Step:2

$( document ).ready(function() { // It run once the page DOM is ready
                  $.ajaxSetup({async: false});
                  userDefinedFunction();
                 $.ajaxSetup({async: true});

                 alert('outside ajaxsetup');
 });

function userDefinedFunction()
{
  alert('inbetween ajaxsetup');
}


Step: 3
call it in HTML page to see the output. Here is the complete code
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<script type="text/javascript">

$( document ).ready(function() { // It run once the page Document Object Model (DOM) is ready for JavaScript code to execute

 $.ajaxSetup({async: false});
    userDefinedFunction();
$.ajaxSetup({async: true});

alert('outside ajaxsetup');

 });


function userDefinedFunction()
{
  alert('inbetween ajaxsetup');
}

</script>
</head>
<body>
Sample ajax setup example
</body>
</html>




No comments: