I think that the best way, assuming that your AJAX requests are asynchronous, is to add a callback parameter to your function, that will be executed when the request ends.
Since you don't mention any JavaScript library either posted your ajaxfunction
, I suppose that you might be doing raw XHR requests, if so, you could for example:
function ajaxfunction (url, elementId, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 ) {
if ( xhr.status == 200 ) {
document.getElementById(elementId).innerHTML = xhr.responseText;
callback(); // the content has been loaded to the DOM, executing callback
} else {
alert('error');
}
}
};
xhr.send(null);
}
And you could use the function like this (note that I don't recommend at all the use inline JavaScript event binding on HTML):
<a href="this.php"
onclick="ajaxfunction('thisfile.php','thisDiv', otherFn);return false;">link</a>
The recommended way:
<a href="this.php" id="linkId">link</a>
window.onload = function () {
// Event binding...
document.getElementById('linkId').onclick = function () {
ajaxfunction('thisfile.php','thisDiv', function () {
alert('Content retrieved, this is the callback!');
return false;
});
};
// ....
};
For a more comprehensive example, post the code of your ajaxfunction
.