JQuery cache XML
I am trying to create a portfolio site where my parts are in an XML file. I would like to store the XML DOM in a JavaScript variable so that I can access it without making a call to the server every time the user clicks to download a new part.
Now I have:
$(document).ready( function() {
xml = getXML();
});
function getXML() {
var data = $.ajax({
url: '_lib/xml/portfolio.xml',
dataType: 'xml',
success: function(data) { return data; }
});
return data;
}
When using Firebug, if I "console.log (data)" in the anonymous function after "success:", it logs "Document". If I "console.log (xml)" to "$ (document) .ready ..." it logs the XMLHTTPRequest and I cannot access the XML DOM document.
I tried to return "data.responseXML" but it doesn't do anything. I also tried calling a non-anonymous function after "success:" and registering the XML there, and it still appears as the "Document" I need.
The problem seems to occur when I return XML - it changes from "Document" to "XMLHTTPRequest".
Any help would be much appreciated. Thank!
source to share
Try the following:
function getXML() {
var XML = null;
$.ajax({
url: '_lib/xml/portfolio.xml',
dataType: 'xml',
async: false,
success: function(data) {
XML = data;
}
});
return XML;
}
I don't know what $.ajax
' returns ', but I tried similar to yours and it never works. So the above code is what I agree with.
EDIT Just want to emphasize that you must have " async: false
" in the ajax request.
Hope it helps.
source to share