Youss
Youss

Reputation: 4212

Do something after window load

I'm doing a (simple) ajax call on window load:

$(window).load(function () {

$.ajax({
url: someUrl,
type: "get",
dataType: "",
success: function(data) {

           ...........

How can I do something, for instance a function or event after this ajax proces is finished. The problem is I have to append something to the data recieved by the ajax call. But I can't append on window load because the data is still being processed.

Upvotes: 1

Views: 447

Answers (6)

Filippo oretti
Filippo oretti

Reputation: 49813

this should execute your request when page is just loaded.

$(function(){
    $.ajax({
        url: someUrl,
        type: "get",
        dataType: "",
        success: function(data) {
            // do whatever you want with data
        }
    });
});

or you can do:

$(function(){
        $.ajax({
            url: someUrl,
            type: "get",
            dataType: "",
            complete: function(data) {
                // do whatever you want with data
            }
        });
    });

Upvotes: 0

JFK
JFK

Reputation: 41143

You could also do

$.ajax({
 url: someUrl,
 type: "get",
 dataType: "",
 context: document.body
}).done(function() { 
  // do whatever when is done
  onCompleteFunction();
});

Upvotes: 0

oshikryu
oshikryu

Reputation: 249

After

success: function(data) { 
  for (var ii = 0; ii < data.length; ii++){
    building html here 
  }
  your code here
}

you want to enter in your functions within the success function for the ajax call but before the call is complete.

Upvotes: 0

felipeclopes
felipeclopes

Reputation: 4070

You may use the

`$(document).ready`

So it will be similar to this:

$(document).ready(function(){

$.ajax({
url: someUrl,
type: "get",
dataType: "",
success: function(data) {

           //Your code should be here

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55750

You can call that in the success callback function of Ajax request

success: function(data) {

    myFunction() ;   // If function

    $('#elementID').click() // If you want to trigger a click event
}

Upvotes: 3

jbabey
jbabey

Reputation: 46647

Put it in the success handler of the ajax call:

$(window).load(function () {
    $.ajax({
        url: someUrl,
        type: "get",
        dataType: "",
        success: function(data) {
            // do whatever you want with data
        }
    });
});

Upvotes: 3

Related Questions