DecodeURI decodes space as a +

I created Google Custom Search. The logic is that when you search for a user, the result will be displayed on the page, and the page title will be changed to a search query using javascript. I use decodeURI to decode Unicode characters. But the space is decoded as +. For example, if I search for money, it will be decoded as money + creation and it will be displayed as a title. Someone please help solve this. I want to display space instead of the + symbol.

Code

if (query != null){document.title = decodeURI(query)+" | Tamil Search";}</script> 
+4
source share
2 answers

The Google Closure Library provides its own urlDecode function for exactly this reason. You can either use the library, or open source below, as they allow it.

 /** * URL-decodes the string. We need to specially handle '+ because * the javascript library doesn't convert them to spaces. * @param {string} str The string to url decode. * @return {string} The decoded {@code str}. */ goog.string.urlDecode = function(str) { return decodeURIComponent(str.replace(/\+/g, ' ')); }; 
+6
source

You can use the replace function for this:

 decodeURI(query).replace( /\+/g, ' ' ) 
+2
source

Source: https://habr.com/ru/post/1485285/


All Articles