October 9th 2008

Include Url of Page in Mailto Hyperlink

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 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:

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.