Solved

make jquery to wait for .get function

Posted on 2011-09-09
5
552 Views
Last Modified: 2012-05-12
Hello.

I have this function
function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

}

Open in new window


I need to wait until
jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html

Open in new window

has change my div content.

Please HELP.
Thanks
0
Comment
Question by:radugheorghies
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36513815
use $.ajax (or use jQuery.ajaxSetup) to make a synchrone ajax call :

function runAjaxCap(mycode) {
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;
        jQuery("#captchacatcher").html(fileContents); 
}

Open in new window


more info : http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36513838
try:
function runAjaxCap(mycode) {
//force it to wait for get completion
jQuery.ajaxSetup({async:false});
	//$("#captchacatcher")
	var xaa='';
	xaa='parser3.php?code='+mycode;	
	jQuery.get(xaa,function(fileContents){jQuery("#captchacatcher").html(fileContents);});

//restore default so it won't "break/interfere" with other ajax calls
jQuery.ajaxSetup({async:true});

}

Open in new window

0
 

Author Comment

by:radugheorghies
ID: 36513863
Sorry leakim971

something is wrong here:
var fileContents = jQuery.ajax({ "url":xaa "async":false }).responseText;

thanks for answering.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36513877
add a comma after xaa

var fileContents = jQuery.ajax({ "url":xaa, "async":false }).responseText;
0
 

Author Closing Comment

by:radugheorghies
ID: 36513883
Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question