I am creating a variable that contains a string that uses the @ symbol. How can I do this in Javascript without the @ sign being parsed as something different in the url?
Asked
Active
Viewed 68 times
-4
-
I don't understand... `var x = "mystring@test"` will work just fine – sachleen Mar 25 '13 at 02:15
-
What is so special about `@` that you can't just put it in a string? Based on your tags, are you trying to fetch pictures from Flickr? If yes, then try this: `encodeURIComponent(abc@example.com)` – Derek 朕會功夫 Mar 25 '13 at 02:15
-
Are you asking how to URL encode an `@` in a string that will be used for a URL, so the URL will only include safe characters (like URL encoding a blank space with `+`, for instance)? If so, refer to [this question](http://stackoverflow.com/q/332872/1306809). – Matt Coughlin Mar 25 '13 at 02:17
-
question sounds silly without any context. If you are having a problem...explain what the the problem is – charlietfl Mar 25 '13 at 02:51
3 Answers
5
You're probably constructing a URL by concatenating strings:
var ajax_url = 'foo.json?' + key + '=' + value;
Don't do that. Use jQuery's AJAX functions and pass an object instead:
$.ajax({
url: 'foo.json',
type: 'GET',
cache: false, // You might need that
data: {
key: value
},
success: function(data) {
...
}
});
key
and value
will automatically be escaped.

Blender
- 289,723
- 53
- 439
- 496
1
@
is not a special character in strings in JavaScript. If you need it in a string, just put it in the string as you would any other normal character.

icktoofay
- 126,289
- 21
- 250
- 231