Solved

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

Posted on 2011-09-19
3
226 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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 …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

615 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