API - Get URL parameter with jQuery function

0 votes
244 views
added Jul 4, 2018 in API by LC Marshal Captain (25,790 points)
edited Jul 13, 2018 by LC Marshal
$(function() { 
  //create function to get URL pramater
  function getUrlParameter(vali) {
      vali = vali.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
      var regex = new RegExp('[\\?&]' + vali + '=([^&#]*)');
      var results = regex.exec(location.search);
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  };
       //fetch from api
  var propertyApi = 'https://cube.fundmyhome.com/api/property-units.json?property_nid=',
      //get URL parameter with getUrlParameter function
      nidValue =  getUrlParameter('nid'),
      //combine and produce final api source
      fullApiLink = propertyApi + nidValue;
      //to test the output
      console.log(fullApiLink);//example output https://cube.fundmyhome.com/api/property-units.json?property_nid=12

  $.ajax({
    url: fullApiLink,
    dataType: 'json',
    type: 'get',
    cache: 'false',
    success: function(data) {
      $(data["0"]).each(function(index, row) {
        var unitStatus = (data["0"].unit_status),
            statusLabel = $('#main_panel .listing-batch.hidden-sm.hidden-xs');

        if (unitStatus === '0' ) {
          // console.log('no status')
          $(statusLabel).html('Coming Soon');
        } else if (unitStatus === '1') {
          $(statusLabel).html('Unfunded');
        } else if (unitStatus === '2') {
          $(statusLabel).html('Buying Closed. Now Funding');
        } else if (unitStatus === '3') {
          $(statusLabel).html('Funded');
        } else if (unitStatus === '4') {
          $(statusLabel).html('Closed');
        }
      });
    }
  })
});
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...