0

My HTML has this in it:

 <script src="../scripts/jquery-1.8.1.min.js"></script>
 <script src="../scripts/displayTable.js"></script>

The displayTable.js consists of this:

$.ajax({
    url: "https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml",
    type: "Get",
    dataType: 'xml',
    success: function (result) {
        $(result).find('Module').each(function() {
             var name = $(this).attr("name");
            var url = $(this).find('url').text();
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var description = $(this).find('description').text();
            $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
        });
    },
    failure: function() {
    alert("Notify the site owner that the xml file is unreadable.");
    }
});

How would I make it so that I can pass the url as an argument rather than hard-coding it into the js file, this way I can use the same file across many pages on my site?

2
  • create a function, pass url as argument. :) :) Commented Aug 17, 2016 at 21:42
  • displayTable.js should define a function that takes the URL as a parameter. Then you call the function. Commented Aug 17, 2016 at 21:42

2 Answers 2

1

Define a function in displayTable.js, then call it after you load the script.

So diaplayTable.js would contain:

function displayTable(url) {
    $.ajax({
        url: url,
        type: "Get",
        dataType: 'xml',
        success: function (result) {
            $(result).find('Module').each(function() {
                 var name = $(this).attr("name");
                var url = $(this).find('url').text();
                var authors = $(this).find('authors').text();
                var version = $(this).find('version').text();
                var date = $(this).find('date').text();
                var description = $(this).find('description').text();
                $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
            });
        },
        failure: function() {
            alert("Notify the site owner that the xml file is unreadable.");
        }
    });
}

Then you use it as:

<script src="../scripts/jquery-1.8.1.min.js"></script>
<script src="../scripts/displayTable.js"></script>
<script>
$(document).ready(function() {
    displayTable("https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml");
});
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Nothing is displaying, chrome dev tools console says: VM9266 displayTable.js:20Uncaught SyntaxError: Unexpected end of input Which is the very last line. It's probably something simple, but I am missing it.
I added a "}" to close the function, now I'm not getting any errors, but I'm also not getting the table to display.
Is #ModsList already in the DOM when you call the function?
If the original script works as you've written it, I can't see why my version wouldn't work just as well.
Can you provide a link to your page that's not working?
|
0

Here you go,

function callAjax(url){
    $.ajax({
      url: url,
      type: "Get",
      dataType: 'xml',
      success: function (result) {
        $(result).find('Module').each(function() {
             var name = $(this).attr("name");
            var url = $(this).find('url').text();
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var description = $(this).find('description').text();
            $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
        });
      },
      failure: function() {
        alert("Notify the site owner that the xml file is unreadable.");
      }
  });
};

Call the function and pass url as parameter where ever you want to call it.

callAjax("https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml"); 
callAjax("https://google.com"); 

2 Comments

Tried the code, although I get no error within the dev console, I also am not having the table display.
NVM, discovered mistake, see comment on other answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.