0

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?

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
Chittprakash
  • 67
  • 11

2 Answers2

3

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".

Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

Well, you could always encode the url: Encode URL in JavaScript?

Or use some comma-separated string.

Community
  • 1
  • 1
Pieter
  • 3,339
  • 5
  • 30
  • 63