views:

82

answers:

4

I'm stuck in a rut. I hope some one can help.

Basically, I am building an AJAX mobile web app with jQuery. I am able to parse a specific XML file just fine, but I want the option to parse other XML files based on the link they were clicked on and load them on the fly into the same DIV or UL.

So:

click on Link1, loads XML1
click on Link2, loads XML2

I would like to be able to do this all client side, so no PHP (or is that a bad idea?). This the jquery code I've been using:

$(document).ready(function() {          

        $("a.load_ajax").click(loadAjax());

        function loadAjax() {
            var fileID = get('?lineID=');

            var dataID = "xml/" + fileID + ".xml"

            $.ajax({
                type: "GET",
                url: dataID,
                dataType: "xml",
                success: parseXml
            });

            function parseXml(xml) {
                $(xml).find("train").each(function() {
                    $("ul#ajax-output").append('<li>' + $(this).find("time").text() + '</li>');
                });
            }
        }           

});

Its just not working at all. I have been passing the variable using GET in the url. So the link in the HTML goes to /?lineID=SBD_to_Union and it should load the XML file called SBD_to_Union.xml

Making sense to anyone? I'd appreciate some help.

+1  A: 

What you seem to be struggling with is obtaining the line from the url in the anchor. Use $(this) to get the href attribute of the clicked link. You could then use a regex, if the url is as described, to remove all but the line id to use in constructing the link for the XML. I presume that the XML is server side and relative to the current url. If not, then you'll need to adjust the path. I've taken the liberty to compress things a bit by putting the functions inline. Edit: and the click handler should return false to prevent the link from actually performing its default action.

$(function() {          

    $("a.load_ajax").click( function() {
        var fileID = $(this).attr('href').replace(/.*?lineID=/,'');

        var dataID = "xml/" + fileID + ".xml"

        $.ajax({
            type: "GET",
            url: dataID,
            dataType: "xml",
            success:  function(xml) {
                $(xml).find("train").each(function() {
                    $("ul#ajax-output").append('<li>' + $(this).find("time").text() + '</li>');
                });
            }
        });
        return false;
    });          
});
tvanfosson
This did it, thanks a lot!!!
Scott
A: 
<a href="#" id="link1" class="load_ajax">Link1</a>
<a href="#" id="link2" class="load_ajax">Link2</a>

You can do things depending on the id of the href (or another of its attributes or its href value).

$(function() {
    $("a.load_ajax").click(loadAjax); 
});

function loadAjax()
{
  if ($(this).attr("id") == "link1")
  {
    alert("link1"); //load xml1
  }
  else
  {
    alert("link2"); //load xml2
  };
}
CRice
... and if you have 50 links ?
Gaby
there aren't 50 links, only 2 in this example so why write a 50 links solution?
CRice
I think Gaby's point was that this is not really scalable...
bobsoap
because his question includes an example, and not the actual html of his page..
Gaby
True, well I was just trying to give Scott some ideas. The rejex answer would be scalable in any case.
CRice
@Gaby @bobsoap @CRice, Before my computer dies out, this is a link to how real men solve the aforementioned problem of scalability - http://jsfiddle.net/eF8GC/
Anurag
:) you have far too much spare time.
CRice
@anurag .. lolol... too much free time :p
Gaby
Lol. You put a javascript in a javascript so you can javascript while you javascript.
bobsoap
+1  A: 

Have you checked if the get function returns the correct data ?

add an alert(fileID); right after the get(..); line..

But why don't you create the urls to point directly to the xml files instead of parsing and creating urls on the fly ?

just make the link in the html to point to xml/SBD_to_Union.xml

Gaby
If javascript is turned off, the URL might load a different page with the correct data. That was my assumption anyway.
tvanfosson
Because I want to be able to use other XML files later on with the project, not just this one.
Scott
@Scott - I think he's suggesting that the link href **be** an XML path that can be used directly.
tvanfosson
@scott, i just used your example.. i mean that instead of making each link point to `?lineID=somexmlforthislink` make it link directly to `xml/somexmlforthislink.xml`
Gaby
+1  A: 

On first glance, I think your ajax() syntax is a little off.

Are you using query strings for a particular reason? If no, I'd try giving the HTML links the absolute URL to the XML file you are trying to fetch:

<a href="xml/file123.xml"></a>

Then try this:

$("a.load_ajax").click(function(e) {
 e.preventDefault();
 var url = $(this).attr('href');

            $.ajax({
                type: 'GET',
                url: url,
                dataType: 'xml',
                success: function(response) {
                    $(response).find('train').each(function() {
                        $('ul#ajax-output').append('<li>' + $(this).find('time').text() + '</li>');
                }
            });

        });

I haven't tested this, but it should do what you want.

bobsoap