resopollution
resopollution

Reputation: 20350

How do I pass the # symbol as part of a GET querystring in the URL?

I am using javascript (using jquery) to pass a # symbol as a GET parameter via AJAX call.

The problem right now is that the # symbol is breaking up my querystring.

Any help appreciated. Thanks!

Upvotes: 0

Views: 3244

Answers (4)

Justin Ethier
Justin Ethier

Reputation: 134177

You need to replace it with %23 in the string. However, instead of doing this directly you should use Javascript function encodeURIComponent to encode characters in the URL.

Alternatively, if you are using jQuery.ajax you can automatically encode parameters by passing them in via the data option.

Upvotes: 7

Chetan S
Chetan S

Reputation: 23803

If you are passing the data parameter to jQuery.ajax, jQuery does the encoding for you. You shouldn't be trying to build the URL yourself.

$.ajax({ url : "http://myserver.com/mypage.aspx",
         data : {'key1' : 'value#', 'key2' : 'value&&'}
         ...
      });

Upvotes: 4

Sarfraz
Sarfraz

Reputation: 382706

You will have to encode the url using escape, encodeURIComponent function, etc to pass that character. This will turn the characters into their % counter-partts. For example, # will be %23

Upvotes: 1

AlexV
AlexV

Reputation: 23098

Use encodeURIComponent().

Upvotes: 2

Related Questions