-2

I have a question. In this code i use +tag to set a search words when user speak in microphone.

var searching = function(tag) {
            var string = "Procurando "+tag;
            var str = string.split("");
            var el = document.getElementById('liya');
            (function animate() {
                str.length > 0 ? el.innerHTML += str.shift() : clearTimeout(running); 
                var running = setTimeout(animate, 30);
            })();

            setTimeout(function () {
                $(location).attr('href', 'https://yazui.co/liya/search.php?q='+tag)
            }, 3000);            
        };

It's ok! But in results, url showing with %20 when have many words

search.php?q=words%20words%20words

How can i change %20 to + in this code?

I need that url returns

search.php?q=words+words+words
  • You URL is being [encoding](https://www.w3schools.com/tags/ref_urlencode.asp), you just need to decode it. FYI `%20` is a space character and not a `+` – George Nov 05 '18 at 15:37
  • 1
    https://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string `tag.replace(/ /g,'+')` – epascarello Nov 05 '18 at 15:38
  • Possible duplicate of [Encode URL in JavaScript?](https://stackoverflow.com/questions/332872/encode-url-in-javascript) – Pete Nov 05 '18 at 15:41
  • 2
    Why do you need to do this replacement? Both `%20` and `+` will be decoded to space. – Barmar Nov 05 '18 at 15:42
  • because my search understand word+word+word and not understand word%20word%20 – Bruno R. Trocoli Nov 05 '18 at 16:14

1 Answers1

0

You can do it like this.

var searching = function(tag) {
            var string = "Procurando "+tag;
            var str = string.split("");
            var el = document.getElementById('liya');
            (function animate() {
                str.length > 0 ? el.innerHTML += str.shift() : clearTimeout(running); 
                var running = setTimeout(animate, 30);
            })();

            setTimeout(function () {
                $(location).attr('href', 'https://yazui.co/liya/search.php?q='+tag.replace(/%20/g, "+");)
            }, 3000);            
        };
Sreekanth MK
  • 233
  • 3
  • 12