some does and don't regarding your issue:
don't use escape()
, its been deprecated since ECMAScript v3.
Use encodeURI()
instead. you can also use decodeURI()
in the way round.
don't use encodeURIComponent()
to escape all the url string, since it would ruin your URL, use it just when you want to send a url as a paramter.
- don't use
'
for the attributes in your json, use "
instead, because the mentioned functions does not encode the '
character.
for instance, you can add your json to the url, and then call encodeURI
:
var url = 'http://myhost.com/?{"key", "value"}';
var myEncodedurl = encodeURI(url);
//result-->http://myhost.com/?%7B%22key%22,%20%22value%22%7D
but
encodeURIComponent(url);
//result-->http%3A%2F%2Fmyhost.com%2F%3F%7B%22key%22%2C%20%22value%22%7D
then using encodeURIComponent
would help you if you want to send all that url
as a parameter, like this:
var myEncodedurl = 'http://myotherhost.com/?myurl=' +
encodeURIComponent('http://myhost.com/?{"key", "value"}');
//result-->http://myotherhost.com/?myurl=http%3A%2F%2Fmyhost.com%2F%3F%7B%22key%22%2C%20%22value%22%7D
BTW, for your sample you'd better try:
var url = 'http://www.facebook.com/plugins/registration.php?client_id=183620578322567&redirect_uri=' +
encodeURIComponent('http://example.com/store_user_data.php?&'
+ 'fields=['
+ '{"name":"name"},'
+ '{"name":"email"},'
+ '{"name":"password"},'
+ '{"name":"gender"},'
+ '{"name":"birthday"},'
+ '{"name":"captcha"}'
+ ']&"'
+ 'scrolling="auto"'//extract it from encodeURIComponent if they belong to your original url
+ 'frameborder="no"'//extract it from encodeURIComponent if they belong to your original url
+ 'style="border: none;"'//extract it from encodeURIComponent if they belong to your original url
+ 'width="500"'//extract it from encodeURIComponent if they belong to your original url
+ 'height="600"'/*extract it from encodeURIComponent if they belong to your original url*/);