用代码替换url中的空格

I have this code.

Which I have set up to promote the page on my site.

<script ... > 
document.write('<a href="mailto:?subject=my%20subject&body='+top.location.href+'">mail this link to a friend</a>'); 
</script>

However in my url it has a space on most of my campaigns. This breaks up the url in my promotional email so the http:// up to the first space is hyperlinked.

How do I replace the space in my url for the space code in the code above meaning that the whole link will be hyperlinked in the email?

You have to encode url first, eg. using encodeURIComponent function.

encodeURIComponent(top.location.href);
<script ... > 
var url2 = encodeURIComponent(top.location.href);

document.write('<a href="mailto:?subject=my%20subject&body='+url2 +'">mail this link to a      friend</a>'); 

document.write('<a href="mailto:?subject=my%20subject&body=' + encodeURIComponent(top.location.href) + '">mail this link to a friend</a>');