@ and . are not special characters in HTTP URIs.
The characters that need encoding include space (which becomes +), + itself, and %, which is used to hex-encode.
Other characters outside the normal ASCII range of 32 - 127 and various other characters within that range use that % hex encoding.
For correct handling, you should consider using encodeURIComponent() but only on the part of the URI that was user supplied. If you encode the entire URI that way you'll get an invalid URI.
@ and . are not special characters in HTTP URIs.
The characters that need encoding include space (which becomes +), + itself, and %, which is used to hex-encode.
Other characters outside the normal ASCII range of 32 - 127 and various other characters within that range use that % hex encoding.
For correct handling, you should consider using encodeURIComponent() but only on the part of the URI that was user supplied. If you encode the entire URI that way you'll get an invalid URI.
You can try using encodeURIComponent instead.
encodeURIComponent('GET http://dev:8989/SJMUserManager/Service/Index/[email protected]')
Return:
"GET%20http%3A%2F%2Fdev%3A8989%2FSJMUserManager%2FService%2FIndex%2Fbmackey%40foo.com"
You could accomplish this using the following way :
- use decodeURIComponent() to decode the URI
- use a
regexto extract the email address
var string = 'https://opt.portal.co.uk/index/stepregistration/Username/test%40test.es/nextstep/1/lang/es?kmi=K54Nv1RdlV71hhiLEdPg0UZ0%3D&_ga=1.217245974.18890806.1485212';
var email = decodeURIComponent(string).match(/\w+@\w+\.\w+/g)[0];
console.log(email);
decodeURIComponent(string)
example from w3c:
var uri = "https://w3schools.com/my test.asp?name=ståle&car=saab";
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
var res = uri_enc + "<br>" + uri_dec;
https://www.w3schools.com/jsref/jsref_decodeuricomponent.asp
In your example you'll want to extract the username from the url. Querystring or Hash key/value pairs might be easier to deal with but you can use a regular expression or split the url by '/' and loop the result to find the element after the 'Username' element.
var encoded_body = encodeURIComponent(window.location.href);
var encoded_subject = encodeURIComponent(document.title);
var mailto_link = "mailto:?subject=" + encoded_subject + "&body=" + encoded_body;
should do it (encodeURIComponent instead of encodeURI).
In your original code you were also incorrectly double encoding the subject (once on line 2, and then again on line 3).
I took the liberty of renaming your variables to make it clearer that they contain the encoded subject and body, as opposed to the original text.
You want encodeURIComponent not encodeURI.
I agree with Matt Greer in the comment above.
Personally, I would not make any emails readable online even via javascript. If it's required for any project then make it visible to registered members only. If your registration process is secure enough and spam free then so will the emails.
Then a basic javascript implementation such as the above will be quite secure.
You could use a script like this. Any bot that can run Javascript will still have access to the email, but since the <a> tag and email address aren't visible anywhere in the source code, bots which just download the HTML and then scan it for email addresses will not notice the email address.
<script>
(function(d){
var s = d.getElementsByTagName('script');
s = s[s.length - 1];
var a = d.createElement('a');
a.href = rot13('znvygb') + '\x3a' +
rot13(arguments[2]) + '\x40' + rot13(arguments[3]);
a.innerHTML = arguments[1];
s.parentNode.insertBefore(a, s);
function rot13(s){
return s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
}
})(document, 'Send Email', 'wbuaqbr', 'tznvy.pbz');
</script>
JSFiddle