1

I had a code that works but the problem is it just append the parameter
instead of replacing it with a new parameter, Here is my working code:

jQuery('#input_1_11').change(function(){

        var type = jQuery('#input_1_11').val();
        if(type=='Inspection'){

            window.location.replace(window.location.href + "?Inspection");

        }
        else if(type=='Installation'){

            window.location.replace(window.location.href + "?Installation");
        }
        else{

            window.location.replace(window.location.href + "?Maintenance");
        }


    }); 

what currently happening is for example

www.test.com?Inspection 

Then if I click again the input11 it goes to the link

www.test.com?Inspection?Maintenance

What I need is to replace each ?parameter every change by user.

1
  • Try: window.location.hostname + "?Inspection" ? Commented Dec 14, 2020 at 9:08

2 Answers 2

2

use origin:

window.location.origin + "?Maintenance" instead of window.location.href + "?Maintenance"

Sign up to request clarification or add additional context in comments.

4 Comments

Note that origin only returns the domain. You'll most likely need to append the path to it as well, along with adding the ? character.
At the end of OP question this is exactly what he is asking for.
True, but without the addition of path the solution is very brittle and easily broken. Regardless of that, this is the best method available; much better than hacking the URL around as a string.
I know, this is why i put the link to the window.location.origin for him to adjust the solution to his needings.
1

You can do a thing - get the present url and split it into an array with ? as parameter for split. Get the first element of that array. It will be the url without parameters.

jQuery('#input_1_11').change(function(){
        var type = jQuery('#input_1_11').val();
        var present = window.location.href.split("?");
        if(type=='Inspection'){
            window.location.replace(present[0] + "?Inspection");
        }
        else if(type=='Installation'){
            window.location.replace(present[0] + "?Installation");
        }
        else{
            window.location.replace(present[0] + "?Maintenance");
        }
});

Comments

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.