Steaphann
Steaphann

Reputation: 2777

execute a function on page load in jquery mobile

I am building a mobile app with Jquery mobile. What you need to know is that I am also working with a content renderer. So I only have one with data-role page. This is what I do in the content renderer. with <%= incBody %> I get the content of my pages.

<body <%=incBodyAttr%>>

<div data-role="page" class="type-index" data-theme="g">

<%=incBody%>

</div>

</body>

I think that was somewhat that you needed to know. Now the real problem. At the moment I have a function load() You can see it over here.

function load(){
var userId =  $("#userId").val();
$.ajax({
            url: "~SYSTEM.URL~~CAMPAIGN.URL~/SelligentMobile/Webservice/WebService.asmx/getNieuwtjes",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'userId':'" + userId + "'}",
            success: function (response) {       
            var nieuwtjes = response.d;
              if (nieuwtjes.length > 0) {   
                        $.each(nieuwtjes, function (i, entity) {
                    $('#nieuwtjesList').append(
                            $("<li/>").append($("<a/>")
                        .attr("href",'~PROBE(239)~&NEWSID=' + entity.nieuwtjeId)
                    .text(entity.nieuwtjeOnderwerp)
                    )
                    );
                    $('#nieuwtjesList').trigger("create");
                        $('#nieuwtjesList').listview('refresh');     
                  });
            } 
        }        
      });
}

Now this load is triggered by a button at the moment. But what I want to do is that each time the page loads, its executing this function.

Can anybody help ?

kind regards

Upvotes: 1

Views: 3214

Answers (2)

nnnnnn
nnnnnn

Reputation: 150080

Call it from a document ready handler:

$(document).ready(function() {
   load();
});

Or, given that you're not passing parameters to load():

$(document).ready(load);

The first way allows you to do other stuff before or after calling load(), should you need to: just add more code into the anonymous function.

See the .ready() doco.

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

You should use jQuery DOM ready:

$(function() {
  // call load() after DOM ready
  load();
});

You can also use as

$(document).ready(function() {
  load();
})

Upvotes: 1

Related Questions