简体   繁体   English

如何使用Jquery动态添加属性值?

[英]How to add dynamically attribute value using Jquery?

I have logic where i am trying to add host url dynamically so it work in all env based on hosst, so below trying to find a file that is there in host but its never going into $.each statement , if call url directly http://18.35.168.87:6000/Sdk/wrapper-sdk/client/out.json it worked and rendered data, any idea what could have wrong in below code to achieve this task ? 我有逻辑,我试图动态添加主机url,以便它在基于hosst的所有环境中都能正常工作,因此在下面尝试查找主机中存在的文件,但从未进入$.each语句,如果直接调用url http://18.35.168.87:6000/Sdk/wrapper-sdk/client/out.json它可以工作并呈现数据,您知道下面的代码中有什么问题可以实现此任务吗?

main.js main.js

function myFunction(val) {
    var url = "../../wrapper-sdk/" + client + "/out.json";
        if (window.location.hostname.indexOf("localhost") !== -1 ||
            window.location.host.indexOf("localhost") !== -1) {
            var scripts = document.getElementsByTagName('script');
            var host = '';
            $.each(scripts, function (idx, item) {
                if (item.src.indexOf('Sdk/wrapper-sdk') !== -1 && (item.src.indexOf('out.json') !== -1)) {
                    host = item.src.split('?')[0];
                    host = host.replace('wrapper-sdk/' + client + '/out.json', '');
                }
            });

   url = url.replace('../../', host);
        }
        $.ajax({
            url: url + "?no_cache=" + new Date().getTime(),
            dataType: "json",
            "async": false,
            success: function (data) {
                console.log(data);
            },
            error: function () {
                console.log('error while accessing api.json.')
            }
        });
}

I would suggest breaking up some of your checks into their own function. 我建议将您的一些检查分解成自己的功能。 Makes it just a bit easier to follow the logic. 使遵循逻辑变得容易一些。

function validIp(str) {
  var parts = str.split(".");
  var result = true;
  $.each(parts, function(i, p) {
    if (parseInt(p) > 0 && parseInt(p) < 255) {
      result = result && true;
    }
  });
  return result;
}

function checkLocalUrl(str) {
  var result = 0;
  if (str.indexOf("localhost") >= 0) {
    result = 1;
  }
  if (validIp(str)) {
    result = -1;
  }
  /*
  0 = Some Domain or Host Name, not LocalHost
  1 = LocalHost 
  -1 = IP Address
  */
  return result;
}

function changeSources(client) {
  if (checkLocalUrl(window.location.hostname) || checkLocalUrl(window.location.host) {
      var scripts = $("script");
      var host = '';
      scripts.each(function(i, el) {
        var src = $(el).attr("src");
        var nUrl = new URL(src);
        var pro = nUrl.protocol;
        var hn = nUrl.hostname;
        if (nUrl.pathname.indexOf('/Sdk/wrapper-sdk') == 0 && nUrl.pathname.indexOf('out.json') > 0) {
          host = pro + "://" + hn + "/wrapper-sdk/" + client + "/out.json";
        }
        $.ajax({
          url: host
          data: { no_cache: new Date().getTime() },
          dataType: "json",
          async: false,
          success: function(data) {
            console.log(data);
          },
          error: function() {
            console.log('error while accessing api.json.')
          }
        });
      });
    }
  }
}

See also: new URL() 另请参阅: 新的URL()

You can send a string to checkLocalUrl() and it will return 1 or true if it's potentially a localhost URL. 您可以将字符串发送到checkLocalUrl() ,如果它可能是本地主机URL,它将返回1true It will return 0 or false if it's any other domain pattern or -1 or false if it's an IP address. 如果是任何其他域模式,它将返回0false如果是IP地址,则返回-1false

In changeSources() we can use this to check for local urls and perform the AJAX you defined. changeSources()我们可以使用它来检查本地URL并执行您定义的AJAX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM