Preloading the XML via jQuery or Javascript

1.1k views Asked by At

Hey guys, how can I preload an external XML file in Javascript/jQuery?

This is my XML loader:

jQuery.ajax({
            type: "GET",
            url: dictionaryList,
            dataType: ($.browser.msie) ? "text/xml" : "xml",
            success: function(xml) {
                var xml2 = load_xml(xml);
                var i=0;
                $(xml2).find('wordle').each(function(){
                    $(xml2).find('w').each(function(){
                        var tmpHold = $(this).text();
                        if (tmpHold.substring(0, 1) == letter) {
                            if ($(this).attr('p') == 1) {
                                wordColor = 'color: #693030';
                            } else {
                                wordColor = 'color: #5a5a5a';
                            }
                            $('#wordList').append('<li class="w" style="'+wordColor+';">'+$(this).text()+'</li>');
                        }
                    });
                });
            }
        });
2

There are 2 answers

0
dan_nl On BEST ANSWER

one possibility, and it sounds like this is what you want, would be to send the response document, (xml) above, to a variable that could be processed on-demand at a later time based on some event.

the stored xml document, and the xml processing function, would live in an object, and the xml processing function would be called based on an event trigger rather than the ajax success event. if this doesn't make sense let me know and i can provide some sample code ...

also, i'd recommend adding an error: function to the ajax call if you don't already have one in place.

2
sultan On

I think it's good to keep backend xml generator/retriever script in case if you want to get xml from a different domain.


jQuery.ajax({
    type: "GET",
    url: XML_GENERATE_BACKEND_URL, // data.xml, /generate/xml  etc.
..
..
..

Sultan