JS for mailto: function?

I’m looking for some java script which will help me perform the the following function:-

I want to have a 'mail this page to a friend button on each of my pages which automatically opens up a new e-mail in the users microsoft outlook and insets the page url in the body of the e-mail (using the mailto: function).

I know you would insert a link like this:
<a href=“mailto:?subject=send a page url&body=URL of current page”>Send this page
to a friend</a>

BUT: Is there any way that I can do this (possibly using a JS function) that would automatically enter the URL of the ‘current page’ into body= , rather that hand coding it for every page?

I’ve got over 600 page so handcoding would be a real chore!

Chilli…don’t know if this would work…

<a href=“mailto:?subject=send a page url&body=$PHP_SELF”>Send this page
to a friend</a>

I’ve just tried it as you said and it just inserts
$PHP_SELF into the body.

Any reason for this not working? I host with hostrocket who have PHP on the servers - so that should be ok. Do I need to save the file with a php extension?

Sorry I’m not at all clued up on scripting.

just tried it by changing the file extensions to .php & .php3 an that didn’t work either.

The $PHP_SELF variable will only work if inside php code, and inside an echo statement. Like this:

<?php
echo(“<a href=\“mailto:?subject=send a page url&body=$PHP_SELF”\>Send this page to a friend</a>”);
?>

You can use JavaScript, however, to do this without having to rename your files .php/.php3:


<script language=“JavaScript”>
var ref = document.referrer;
document.write(“<a href=\“mailto:?subject=send a page url&body=” + ref + \”>Send this page to a friend</a>");
</script>

See if that works. :slight_smile:

Hmmm, that doesn’t seem to work, but I’m not sure why. Anyone have any ideas? I’m stumped.

I don’t know if this could help you but I got this short reply when I posted on Webhostingtalk.

document.write('<a href= etc, etc, etc '+top.window.location

My JS is limited to say the least (deamweaver set functions) so I didn’t know exactly how to implement this with the proper tags etc.

This seem to be working for me…however i’m not sure that works with every browser/platform/emailclient…

The script:
<script language=“javascript”>
function Send_this_page()
{
send_it = "mailto:?subject= bla bla bla bla ";
send_it += "&body= check this web page bla bla bla "+ document.location;
document.location = send_it;
}
</script>

And here the call to the function:
<a href = “javascript:Send_this_page()”>Send this page to a friend</a>

If you’re worried about compatibility, just use PHP in the echo command I mentioned earlier - compatible with anything that even resembles a browser. :slight_smile:

This will open the standard email client; grab the title and url of the page and place them in the email body; insert the e-mail address entered in the <form> text box in the email To: box; and place a message in the email Subject line.

<html>
<head>
<title>This is a page</title>
</head>
<body bgcolor=“#ffffff” text=“#000000” id=all>
<form name=“Mailer”>
E-MAIL THIS LINK
<br>
Enter recipient’s e-mail:
<br>
<input type=“text” name=“address” size=“25”>
<br>
<input type=“button” value=“Send this URL” onClick=“mailThisUrl();”>
</form>
<script>
u = window.location;
m = “I thought this might interest you…”;
function mailThisUrl(){
window.location = “mailto:”+document.Mailer.address.value+“?subject=”+m+“&body=”+document.title+" "+u;
}
</script>
</body>
</html>

Take care to get the entire window.location = "mailto… etc. in the function all on one line.

Edited by etLux on 11-23-2000 at 03:43 AM