2

try to run below url :

http://seaway.fulba.com

search word :- Hello Hi 

Chrome add special character %20 instead of space, I want keep word as it is (Hello Hi) in the url what ever is search there.

I am passing url using html5 pushstate function. Please help me to solve this issue.

r4sn4
  • 117
  • 5
  • 14
amit
  • 47
  • 1
  • 2
  • 10
  • 2
    spaces are not valid in urls. Any browser that is showing spaces is just encoding them to %20 in the background. In your code you would reverse this encoding – Steve Aug 13 '14 at 10:14
  • @amit , It's not possible to set space as `" "` in address bar. Use decodeURI function in your javascript. Problem solved. – Temüjin Aug 13 '14 at 10:16
  • take a look at this link: http://www.w3schools.com/tags/ref_urlencode.asp – lexmihaylov Aug 13 '14 at 10:17

2 Answers2

2

Show ur your code or try urldecode() for this like,

$url=urldecode($yoururl);
echo $url;

And when passing your search string in pushstate use window.unescape or decodeURI()

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Good. But he is using in html5 pushstate. So, put javascript for his. up vote for `decodeURI()`. – Temüjin Aug 13 '14 at 10:18
  • @amit show me your code of search button click which changes your url(pushstate) – Rohan Kumar Aug 13 '14 at 10:31
  • You need to use decodeURI in `document.ready()` like `$('#gsc-i-id1').val(decodeURI(param));` currently it is not working in FF too. – Rohan Kumar Aug 13 '14 at 10:45
  • function search_load() { $( ".custom" ).remove(); //$( "#custom").html( " " ); searchme=$("#gsc-i-id1").val(); if ($('#showImages').is(":checked")) { showImages=$("#showImages").val(); } else{ showImages="no"; } ajax function $(".banner").show(); if(searchme!=window.location){ window.history.pushState({path:searchme},'',"/?r="+searchme); } return false; } – amit Aug 13 '14 at 10:52
  • before i pushstate url it is alert ok like hello hi, but after pushing url it added special character there – amit Aug 13 '14 at 11:23
  • Try to use `window.unescape` instead of `decodeURI`. See http://www.w3schools.com/jsref/jsref_unescape.asp – Rohan Kumar Aug 13 '14 at 11:31
2

That is not possible since space is an invalid character in a URL/I.

The ASCII hex code of the space character 20, therefore you see the encoded space as %20and an encoded ' as %27.

To reuse this in javascript you should use:

var str = decodeURI(URL_STRING);

In your case that would be:

var urlRValue = window.location.search.split('=')[1];
// "Hello%20Hi"
var str = decodeURI(urlRValue);
// "Hello Hi"
Matyas
  • 13,473
  • 3
  • 60
  • 73
  • can you more specify it. is it possible what exactly i want? because it is working good in mozila – amit Aug 13 '14 at 10:28
  • Sorry, but I beg to differ, if you take a look at the value of `window.location.search.split('=')[1]` in firefox, it displays also "Hello%20hi". Or another test: copy the url and paste it into notepad (or any plain text editor), you will see the `%20`. Firefox internally transforms the %20 and **only shows** it as a ` ` (space) character. But if you access it via `js` you'll always get `%20` – Matyas Aug 13 '14 at 10:33
  • so above functioan can be achieve my goal to keep space as it is in chrome ? – amit Aug 13 '14 at 10:46
  • Again: There are no spaces in a `url`, only `%20`-s. Firefox does some magic to show ` ` (space).`decodeURI` transforms encoded urls into normal strings `decodeURI('%20')` produces `" "`. Read more on: [decodeURI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) or [`decodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) and the [differences between them](http://stackoverflow.com/questions/747641/what-is-the-difference-between-decodeuricomponent-and-decodeuri). Maybe that will help you – Matyas Aug 13 '14 at 11:10