Tonight I was trying to add the URL of the current page in a mailto hyperlink. It seamed easy enough until I tried a page that needed a QueryString. I could get the URL just fine, but the QueryString just would not work. It took a second to realize that that the "?" in the QueryString was causing the problem. Now, how do I encode it. A little searching found http://email.about.com/cs/standards/a/mailto_js.htm
JavaScript has a build in function encodeURIComponent that works great.
jQuery:
<script type="text/javascript">
$(document).ready(function() {
$(".emailLink").click(function(ev) {
ev.preventDefault();
var path = encodeURIComponent(window.location);
var lnk = $(this).attr("href") + " - " + path;
window.location = lnk;
});
});
</script>
HTML:
<a href='mailto:example@example.com&Subject=Check out this page!&Body=This is the link - ' class="emailLink">Email</a>
Update (10OCT08):
So, I woke up this morning and the first thing I thought about was that it would be better to update the link on document ready instead of when it is clicked. This way, if you hover over the link you see exactly what your getting plus there are no issues with changing the window's locations.
<script type="text/javascript">
$(document).ready(function() {
$(".emailLink").each(function(ev) {
var path = encodeURIComponent(window.location);
var lnk = $(this).attr("href") + " - " + path;
$(this).attr("href", lnk);
});
});
</script>