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.

1 Answer 1

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','');
Sign up to request clarification or add additional context in comments.

2 Comments

It works thanks. But how can I change the posted URL to to reflect the prices?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.