开发者

Error in Query String.Whitespaces

开发者 https://www.devze.com 2023-04-08 23:53 出处:网络
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>;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号