I think I'm missing something really basic here, so please point me in the right direction!
jquery mobile is loading my pages, but in addition to this on each page I have a secondary ajax call which loads a section of the page.
The first pageload works fine (as you'd expect), the ajax call loads and applies the results to the correct div. However when I navigate to another page on my site, my ajax call fires, I get a response, I select the div however it's not applying the result of the ajax call to the html content of the div, which I find a little odd.
I call the setupLogin()
function in the head and it does fire correctly on every pageinit:
function setupLogin() {
console.log('calling my ajax...');
var request = $.ajax({
url: '/ajax-file.php',
type: 'GET',
dataType: 'html'
});
request.done(function(msg) {
console.log('got ajax result: '+msg);
$('#page-header').html(msg); // <- this is doing nothing after initial pageload
var div = $('#page-header'); // <- so I added this line
console.log(div); // <- and this line to check it's grabbing the div ok
});
}
$(document).delegate("body", "pageinit", function(event) {
setupLogin();
});
The console log lines output exactly as I'd expect:
console.log('got ajax result: '+msg)
logs the correct output of the /ajax-file.php
call, so I know the ajax call is working perfectly.
console.log(div)
logs:
[div#page-header, context: document, selector: "#page-header", constructor: function, init: function, selector: ""…]
0: div#page-header
context: document
length: 1
selector: "#page-header"
__proto__: Object[0]
Which I assume means it's found the div correctly, and I get no errors at all. Everything loads ok, apart from the contents of msg
aren't added to the html content of $('#page-header')
in the $('#page-header').html(msg);
line.
I assume it's something to do with how jquery loads the page? Could it be applying the $('#page-header').html(msg);
line to the previous page, before it's loaded the new page into the DOM? In which case how do I fire this event after everything's fully loaded? I thought that was what pageinit
did, or am I mistaken?
UPDATE
@Brad M The contents of the ajax response is just pure html:
<div id="page-header-top">
<div id="h_logo"><a><span id="redLogo"></span></a></div>
<div id="h_login"><a href="/w/touch/banking"><span id="bankingBtn" class="">Banking</span></a></div>
<div id="h_games"><a href="/w/touch/games/"><span id="allGamesBtn" class="">All Games</span></a></div>
<div id="h_home"><a href="/w/touch/home"><span id="homeBtn" class=""></span></a></div>
</div>
<div id="page-header-bottom" class="small"><b>User Name £0.00</b><a href="/?logout=true" data-ajax="false"><span class="logout-btn">Log out</span></a></div>