1

code:

$(document).ready(function(){
        $("#search_result").change(function(){
            str = $(this).val();
            search = str.replace(/%20/g, "+");
            window.location.href = "courses.php?search-result="+search
        });
    });

In this code I have a dropdown where I have multiple values and now I want to pass dropdown values in url using window.location.href. Now, values are passing perfectly but I want to replace (%20) with (+). I am using str.replace function but not able to see any changes. So, How can I get my url like.

courses.php?search-result=core+java
omkara
  • 974
  • 5
  • 24
  • 50
  • Why do you want to do that? :-) *"courses.php?search-result=core%20java"* is a totally valid URL as well – Philipp Maurer Mar 28 '18 at 09:42
  • 1
    Because I need this @PhilippMaurer :) – omkara Mar 28 '18 at 09:44
  • From what you were able to describe, perhaps you want to have it like this: str.replace(/ /g, '+') The inital string has actual spaces instead of escaped ones (%20). hence str.replace(/%20/g, "+") does not affect it – kurideja Mar 28 '18 at 09:47
  • Have look here: https://stackoverflow.com/questions/4292914/javascript-url-decode-function#answer-4458580 It 's all about url decoding with javascript. – Marcel Mar 28 '18 at 10:08

1 Answers1

0

The solution is decoding the url. The answer is given already here: JavaScript URL Decode function.

Marcel
  • 4,854
  • 1
  • 14
  • 24