简体   繁体   English

jQuery到AJAX请求的Javascript转换

[英]jQuery to Javascript Conversion for AJAX request

Looking to convert a jQuery snippet to javascript. 希望将jQuery代码段转换为javascript。 Totally hitting a wall with this. 完全撞墙了。

Are there any automated tools for doing this? 有自动化的工具吗?

The reason we're converting this is because the code is used as part of a product that we're planning on injecting in the future and we can't guarantee that the site has the appropriate (or any) version of jQuery running. 之所以要进行此转换,是因为该代码被用作我们计划在将来注入的产品的一部分,并且我们不能保证该站点能够运行适当(或任何)版本的jQuery。

EDIT: The reason we can't control the use of jQuery is that the customer will be copying and pasting this code themselves. 编辑:我们无法控制jQuery的使用的原因是客户将自己复制和粘贴此代码。 It's highly likely that they will be non-technical and as such not willing/able to setup the appropriate version of jQuery 他们极有可能是非技术性的,因此不愿意/无法设置合适的jQuery版本

This is the jQuery code we're using now: 这是我们现在正在使用的jQuery代码:

$(function() {
    $.getJSON('http://api.seedthechange.org/get/?method=getCount&str=suitjamas&jsoncallback=?', function(data) {
          treecount = csn(data[0]); 
          $('span.SeedTheChangeCount').text(treecount); 
      });
})

function csn(val){
    // convert int to comma separated number (1000 -> 1,000)
    while (/(\d+)(\d{3})/.test(val.toString())){
        val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
}

Thanks in advance for any help or tips. 在此先感谢您的帮助或提示。

The best reference for what you ask is: http://youmightnotneedjquery.com/ 您所要求的最佳参考是:http: //youmightnotneedjdj.com/

In a browser >= IE8 is: 在浏览器中,> = IE8是:

var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);

request.onreadystatechange = function() {
  if (this.readyState === 4) {
    if (this.status >= 200 && this.status < 400) {
      // Success!
      var data = JSON.parse(this.responseText);
    } else {
      // Error :(
    }
  }
};

request.send();
request = null;

These are the basic points for conversion: http://code.tutsplus.com/tutorials/from-jquery-to-javascript-a-reference--net-23703 这些是转换的基本点: http : //code.tutsplus.com/tutorials/from-jquery-to-javascript-a-reference--net-23703

You could write an automated parser that use these rules for conversion. 您可以编写使用这些规则进行转换的自动解析器。 That should do the trick. 这应该够了吧。 I am not aware if someone has already done this. 我不知道是否有人已经这样做了。

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

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