Solved

Is there a way to force next line of code to run after jquery ".load completes"

Posted on 2011-09-19
3
225 Views
Last Modified: 2012-06-27
I am using "$("divIdName").load()" function to populate a div element.  For some reason, I seem to be having a timing issue where the subsequent lines of code seem to be executed before the load completes.  Is there a way I can force the load to complete before the subsequent lines of code are executed?
0
Comment
Question by:rawcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36560122
use : $.ajaxSetup({"async":false});

$.ajaxSetup({"async":false});
$("divIdName").load(.............. // your current code here
$.ajaxSetup({"async":true});

Open in new window


or use the full ajax : $.ajax({ "async":false, "url":"the_url_youve_in_load", function(data) { $("divIdName").html( data ); } });
0
 
LVL 11

Accepted Solution

by:
JohnSixkiller earned 500 total points
ID: 36565217
Hi

you are using AJAX (Asynchronous Javascript and XML). So every request you make is served in background and any following code is executed immediately after the request. This approach will prevent browser to freeze if server response (for any reason) is delayed.

You have two options:

1) Make synchronous request - If you are sure that server response will not take long
use leakim9871s code or
var html = $.ajax({
  url: "your_url",
  async: false
 }).responseText;
$("divIdName").html(html);

Open in new window

See: http://api.jquery.com/jQuery.ajax/

2) Make it really asynchronous
$("divIdName").load("your_url",function(){
  // The rest of your code goes here. It will be executed AFTER request is completed
 ...
});

Open in new window

See: http://api.jquery.com/load/
0
 
LVL 6

Expert Comment

by:jjperezaguinaga
ID: 36570334
Greetings rawcoder,

The best way to solve your issue is to handle whatever's next your load with a function. JohnSixKiller second option is the way to go.

Best regards,
-JJ
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 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