I am creating a hyperlink to a page
The url is determined by the user input,thus by the querystring
<a href='+abc+'&country='+country +'&state='+state+' ></a>;
开发者_开发知识库
The problem is that the variable state consists of two or more words..
so when i try to click the hyperlink proving the input in the form,only the first word of the state variable is fetched.Browser treats the other as another variable.
example if i input new york as state.
in the state variable only new is saved,asnd the browser treates york as another variable with a blank value &york=""
What should I do?
Escape the illegal characters with encodeURIComponent;
'<a href='+ encodeURIComponent(abc)
+'&country=' + encodeURIComponent(country)
+'&state=' + encodeURIComponent(state) + '></a>;
Which would, for example, convert "aaa bbb"
to "aaa%20bbb"
.
Well, you could always encode the url: Encode URL in JavaScript?
Or use some comma-separated string.
精彩评论