Standard AJAX query with jQuery:
var globalTitle = "";
var pages = ["a", "b", "c"];
for (var i = 0; i < pages.length; i++) {
createpage(pages[i]);
}
function createpage(title) {
globalTitle=title;
console.log (globalTitle); //All looks good here
$.ajax({
url: "createpage.php?id=" + title,
context: document.body,
success: success
});
}
The success() function uses globaltitle which is why I had to declare it global.
In the success() function though console.log (globalTitle); constantly gives me "a". It's like the variable is assigned but is then cached every time success() is called.
Happens in FF 4 and Chrome 8. Any thoughts?
Edit: Here's the success() function:
function success(text) {
console.log (globalTitle); // always "a"
var div1 = "<div id=\"" + globalTitle + "\">";
var text = "<a href=\"javascript:createpage('" + globalTitle + "')\">Retry</a> " + +text;
var div2 = "</div>";
if ($("#" + globalTitle).length) {
$("#" + globalTitle).html(text);
} else {
$("#ajax").append(div1+text+div2);
}
}
success()