0

I am using Ajax to load an xml and shape it into a jQuery Mobile listview. Right after sending the xml i am calling the trigger method so that the page can be re-initialized and the css styles can be added to the listview like so:

xmlhttp.send();
$("#page1").trigger( "create" );

When i load the page, i see the styles on the listview for a fraction of a second but then the listview shows up without the jQuery Mobile styles. It seems to work when i use a delay though like so:

setTimeout(function(){
    $("#page1").trigger( "create" );
}, 5);

I noticed this as it always seemed to work when i used a breakpoint. This bothers me though, because when i use this method i always see the unthemed list for a short while. I also tried refreshing the list right before and after creation but none seemed to work. Does anyone know a way to fix this issue?

1 Answer 1

1

AJAX requests are asynchronous, meaning other code runs while the response is being waited-on. The general flow for using an AJAX request is to do any work on the data received in a callback function for the request. Here is an example using jQuery AJAX:

$.ajax({
    url     : 'my-page.html',
    success : function (response) {
        $("#page1").html(response).trigger('create');
    },
    error   : function (jqXHR, textStatus, errorThrown) { /*remember to handle errors*/ }
});

This assumes that the server-response is valid HTML and that you wanted to replace the content of #page1 with the response from the server.

Sign up to request clarification or add additional context in comments.

3 Comments

how do i get this to work when i am using xmlhttp? I am replacing my content like so: document.getElementById('content').innerHTML=rawhtml; xmlhttp.open("GET",url,true); xmlhttp.send();
Sorry I'm no expert with that, I use jQuery for any AJAX I need to utilize. Search Google (or whatever) for something like: xmlhttp ajax callback. That should find you the correct answer.
I tried this using an ajax get from another html page providing me the raw list view, just like your example. However the trigger does not seem to have any effect in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.