1

When the user do a search, I'm passing the user's search word as the string to direct a user to a new page. My question is how to create a valid url (e.g. prevent spaces on the string)?

It's important that the next page is able to identify where search word spaces were from the string (if there were any spaces).

This is what I have so far:

window.location.href = "/furniture/info.html?furn="+user search word
Bekki
  • 719
  • 2
  • 12
  • 20

1 Answers1

3

Javascript has a encodeURIComponent() which can be used for exactly this purpose.

var someString = 'hey there';
var encodedString = encodeURIComponent(someString);
var newURL = 'yourdomain.com/'+encodedString;
user2027202827
  • 1,234
  • 11
  • 29
  • Yep :) You may already be aware, but the 'inspector' in chrome and firefox are great tools for testing this stuff yourself. – user2027202827 Jul 04 '15 at 03:13