Discussion Locked

Java Script

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have written a script in plain Java Script (for cross platform compatibility) to add a refer this page to a friend section on my site. I?m not ready to try a Unix CGI script yet so would like to stick with the JS. Here is the script

The first problem is with the ?&? in this line.

parent.location.href='mailto:'+ email + ??subject? + subject + '&body='+ body + '';

I get an Object expected error. If I remove it and just use the body it will work but with no subject in the e-mail. I think that it needs to be escaped but can?t find it in my books.

The second problem is with terminating the script after the two ?name? alerts.
A simple break wont work even tried to put it in a loop to do that, no go. I also tried to capture the onclose event of the alert window but kept getting Object expected error. Thank you. Points will be given to the first working answer or divided between the two parts.

5 replies

by andrew

Use the escape function to encode the string:

parent.location.href='mailto:' + email + escape('?subject=' + subject) + escape('&body='+ body);

After the alerts, use the return statement to exit the function:

if (email == ""){
window.alert("You need to enter an e-mail address!");
return;
}

by madcateast

I needed to change the line to

parent.location.href='mailto:' + email + ('?subject=') + subject + ('&body=')+ body;

then it worked. Thank you for pointing me in the right direction.

Mike

by jay eckles

You should REALLY use a server-side script like CGI, ASP, or Java servlet/JSP. There are probably scripts already written and available for free - try places like http://www.cgiresources.com - all you'd have to do is configure and install the script.

If you can't find one pre-written (I'd be amazed), I can write one for you for a very reasonable price; probably about $100.

A server-side script would ensure that ALL your visitors would be able to refer freinds to your site, not just ones that have a browser that understands your dubious mailto: URL and have a mail client configured. You also have total control over the message that is sent - with your solution people can write whatever they want in the email.

Please email me at j.eckles@computer.org if you have questions or want to talk about this further.

Jay Eckles
www.jayeckles.com

by madcateast

Hello Jay, I understand what you are saying. I still need to find out from the admin if sendmail is available (for my use) and it?s path. I also want to read a little more to understand what I am doing. You are correct in that there are many scripts out there and looking at them are only helping me learn more. Thank you for your thoughts.

Mike

This discussion is closed to new replies.