1

I have a problem , my original URL looks like this:

test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000

As you can see, the min-price and max-price values in the query string is not correct due to the comma that is passed to the URL. It should be in their respective integer value like min-price=270000 and max-price=780000.

I need to convert the query string values of min-max and max-price using jQuery. I currently do not how to do this actually. But I have codes to get them from the URL and then convert them to the correct value. I just don't know how to implement them back to the URL (as new URL) using jQuery. These are my existing codes:

    //Function to get value of parameter in query string
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
           results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

    //Function to remove commas and convert to number
    function convert_to_pure_number(x) {

    //Remove commas
    var x_withoutcommas=x.replace(/,/g,'');

    //Convert to plain number
    var y =parseInt( x_withoutcommas ,10);

            return y;
    }

    var min_price_original=getParameterByName('min-price');        
    var max_price_original=getParameterByName('max-price');        
    var min_price_converted=convert_to_pure_number(min_price_original);       
    var max_price_converted=convert_to_pure_number(max_price_original);

Any suggestions how will I continue the above code with the additional code to put them back to the URL posted? Thanks for any help.

UPDATE This is the process: Form will be posted to the server--> URL will contain commas --> My new code will remove the comma --> In the query string value, correct value will be used.

Cheers.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Emerson Maningo
  • 2,189
  • 9
  • 32
  • 47

1 Answers1

0

use replace function like this :

  function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
               results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

     var min_price_original=getParameterByName('min-price').replace('%2C','');
     var max_price_original=getParameterByName('max-price').replace('%2C','');
Harshit Tailor
  • 3,261
  • 6
  • 27
  • 40
  • It works thanks. But how can I change the posted URL to to reflect the prices? – Emerson Maningo Jun 27 '13 at 09:59
  • I don't see any `jQuery` code in your answer... perhaps the title question and tag are malformed... @CodexMeridian If this answer is good enought (as it appears), please edit your question and title to accept pure js code as well. – gmo Jun 27 '13 at 12:11