开发者

jQuery Ajax - Alternate XML sources for each request

I have two XML sources to retrieve data from. I want to use them alternately per page load. So when someone visits the page the first source will be used, next time the visit the page the other source will be used. Here is the ajax request I am using to get one data source:

 $(document).ready(function() {
            $.ajax({
                type: "GET",
                url: "source1.xml", //how do I alternately load two different xml data sources?
                dataType: "xml",
                success: function(xml) {
                    var counter = 0
                    var output = '<li>';
                    $(xml).find('person').each(function(){
                        counter++;
                        var image = $(this).find('image').text();
                        var name = $(this).find('name').text();
                        var title = $(this).find('title').text();
                        var company = $(this).find('company').text();

                        output = output + '<div><a href="#"><img src=img/' + image + '.jpg />' + '<br /><label><span>' + name + '</span><br />' + title + '<br /开发者_开发百科>' + company + '</label></a><br /></div>';
                        if(counter % 3 === 0){
                            output = output + '</li><li>';
                        }
                    });
                    output = output + '</li>';
                    $('#update-target ul').html(output);
                }
            });
        });

For extra info, here is how I am alternately loading 2 flash files using PHP:

if(isset($_SESSION['rotation'])){
$picker = $_SESSION['rotation'];
}else{
$picker = rand(0,1);
}

if($picker == 0){
echo '<script type="text/javascript">
    var video1 = new SWFObject("somefile1.swf", "p1", "151", "590", "9", "#ffffff");
    video1.addParam("wmode","transparent");
    video1.write("meh");
</script>';

$_SESSION['rotation'] = ++$picker;
} else {
echo '<script type="text/javascript">
    var video1 = new SWFObject("somefile2.swf", "p1", "151", "590", "9", "#ffffff");
    video1.addParam("wmode","transparent");
    video1.write("meh");
</script>';

$_SESSION['rotation'] = --$picker;
}

I realize I could just stick the jquery document ready code right in there where I have the js calling the flash but it does not seem like a very efficient way of handling this. What is a "best case" way to do this?


You can just use a variable to keep it short, like this:

echo '<script type="text/javascript">var xmlSource = "source1.xml";</script>';

Use that in an if caluse as well, then just reference that in your code:

 url: xmlSource,

There are other ways of course, using a cookie (the cookie plugin), putting the text right in the document.ready handler, etc...whichever seems most elegant to you I suppose.

I recommend the variable from the PHP side or a cookie...both of these options allow the document.ready code to stay outside the page in an external script, and not downloaded by the user each time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜