There is an object:

var url = { first: "/url/to", second: "/url/to/2" } 

There is a function:

 var GetUrl = function(type){ return url[type] } 

And there is a code:

 <div data-ajax data-load="name, type"></div> var $block = jQuery('[data-ajax]'); var $data = $block.data('load').split(','); jQuery.ajax({ url: GetUrl($data[1]) + $data[0], type: "get", dataType: "html", success: function (template) { StopSpinner(); // hide the spinner $block.html(template); }, error: function (error) { $block.html("<p>Произошла ошибка при получении данных</p>"); } }); 

And in the end, you can not get the value of the url object. Why? After all, in the function GetUrl I pass the index to get the value, but it goes andefind ...

  • GetUrl($data[1]) => GetUrl(" type") , GetUrl($data[1]) + $data[0] => "undefinedname" - what would you like to receive as a url value? - Igor
  • @Igor man wrote exactly what he wants to receive, either first url or second url - Raz Galstyan
  • @ Alex_01 - data-load="name, type" - this is what you have in reality in html? - Igor
  • @igor yes, then I split it with a split. I don’t want to shove a date at 2 - Alex_01
  • @Igor need to get the value first - Alex_01

1 answer 1

 var url = { first: "/url/to", second: "/url/to/2" }; var GetUrl = function(type) { return url[type]; }; var $block = jQuery('[data-ajax]'); var $data = $block.data('load').split(','); var url = GetUrl($data[1]); console.log(url); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-ajax data-load="name,first"></div>