omkara
omkara

Reputation: 982

How can we replace %20 with - operator sign from url?

code:

I have created an autocomplete suggestion box which is completely fine. Now, I have redirect my selected item from autocomplete to url and my url look like

mywebsite.com/search.php?aa=Admission%20support

here I want to replace %20 with - operator sign after that my url look like

mywebsite.com/search.php?aa=Admission-support

So, How can I do this ?please help me.

<script>
    $(function() {
        $( "#show" ).autocomplete({
            source: 'search-result.php',
            minLength:2,
            select: function(event, ui) 
            {
                x = ui.item.value;
                y = x.replace(/%20/g, "-");
                location.href = "search.php?aa="+y;
                return false;
            }
        });
    });
</script>

Thank You

Upvotes: 0

Views: 66

Answers (1)

Duc Filan
Duc Filan

Reputation: 7157

%20 is not encoded from -. Javascript has a built-in function to decode:

console.log(
  decodeURI("mywebsite.com/search.php?aa=Admission%20support")
);

Refer w3schools for more information.

Upvotes: 1

Related Questions