Im using a proxy to get Bing search results, based on the value of some input.
var query = $('input').val();
var url = 'http://www.bing.com/search?q=' + query + '';
...rest of code...
It works good except in case of more than one word seperated with space, for instance this will not work: “cars for sale” Because here we have 3 words and they need to be seperated in the url with either “+” or with “%20”
So the actual url will look like this:
http://www.bing.com/search?q=cars+for+sale+
So how do I get the var url to distinguish when it comes to spaces in the input value?
ps The query does not necessarily have to be in input, it can also be plain text in some div
Use
encodeURIComponent. It is the general solution to the “I want to add user input to a URI” problem.This will handle your specific problem as well as issues such as the data entered containing an
&character.