tags:

views:

14

answers:

2

I have the following Jquery ajax call but would like to put as the Url a variable instead of a string.

Is that possible? For instance instead of make10.xml can I have a variable which equals to "make10.xml" or some other string?

$.ajax({
    type: "GET",
    url: "make10.xml",
    dataType: "xml",
    success: function(xml) {
    var select = $('#mySelect');
+2  A: 

Yes

var url = "make10.xml";

$.ajax({
    type: "GET",
    url: url,
    dataType: "xml",
    success: function(xml) {
    var select = $('#mySelect');
ILMV
LOL, I should have just tried that before posting as that is what I thought, silly me.
+2  A: 

Yes, certainly.

var u= 'make10.xml';
$.ajax({
    url: u,
    ...
});

Not really jQuery-related, this is basic JavaScript. The right-hand side of the colon in an object literal can be any value expression including variables.

It's only the left-hand side of an object literal that is unusual: when you say url it's a shorthand for the string literal 'url' instead of referring to a variable called url. If you want to be clearer about it you can write it out in full:

$.ajax({
    'url': u,
    ...
});

though it's arguable whether that's more or less readable.

bobince
Thanks for taking the time to further explain. I appreciate learning "the why" not just the answer.