javascript / jQuery variable problem (possibly caching issue)

Posted by waitinforatrain on Stack Overflow See other posts from Stack Overflow or by waitinforatrain
Published on 2010-12-28T05:43:02Z Indexed on 2010/12/28 5:54 UTC
Read the original article Hit count: 146

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery