0

Looking to convert a jQuery snippet to 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.

EDIT: The reason we can't control the use of jQuery is that the customer will be copying and pasting this code themselves. It's highly likely that they will be non-technical and as such not willing/able to setup the appropriate version of jQuery

This is the jQuery code we're using now:

$(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.

Phil
  • 157,677
  • 23
  • 242
  • 245
Bryce York
  • 956
  • 1
  • 15
  • 35
  • 1
    why u want to convert it into Javascript? why you can't guarantee that it's running which version if jQuery – Arpit Srivastava Feb 04 '15 at 04:03
  • 1
    @ArpitSrivastava because we provide this to non-technical people who won't necessarily want to give us direct access to their site to set it up for them. – Bryce York Feb 04 '15 at 04:05
  • yes, but you can check which version of jQuery they are using by inspecting it in Browser if you able to access from internet/intranet – Arpit Srivastava Feb 04 '15 at 04:09
  • 1
    You could always programmatically check for jQuery and dynamically include it if it's not there. See http://www.sitepoint.com/dynamically-load-jquery-library-javascript/. Otherwise, JSONP is damned tricky – Phil Feb 04 '15 at 04:12
  • 1
    we're trying to build a business though @ArpitSrivastava and that's just not practical. – Bryce York Feb 04 '15 at 04:13
  • I'm not sure why you're being downvoted. To those having trouble understanding, think of it as the Google Analytics script tag. OP will be distributing a code snippet for people to add to their site – Phil Feb 04 '15 at 04:14
  • thanks @Phil, that could definitely be an option. I'm guessing we could use a polyfill of some sort if they have an older version than we need? – Bryce York Feb 04 '15 at 04:14
  • 1
    Assuming the API is yours, have you considered opening it up via CORS headers so you don't have to rely on JSONP. Making a straight XHR would be much simpler – Phil Feb 04 '15 at 04:17
  • That looks like it could make life a lot easier for us @Phil. It looks like this would be quite easy for our backend guy to do in our Django app. I'm assuming limiting it to GET requests only would be sufficient security protection? – Bryce York Feb 04 '15 at 04:23
  • 1
    I'd also take a leaf out of the GA script and put your code in a JS file you host, have them include it and call some functions. See https://developers.google.com/analytics/devguides/collection/gajs/#quickstart – Phil Feb 04 '15 at 04:26

2 Answers2

0

The best reference for what you ask is: http://youmightnotneedjquery.com/

In a browser >= IE8 is:

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;
Johann Echavarria
  • 9,695
  • 4
  • 26
  • 32
0

These are the basic points for conversion: 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.

kung_fu_coder
  • 146
  • 2
  • 3