View Full Version : Ampersand trouble in JavaScript

06-14-2004, 03:44 AM
The script below sends the link of the current page in an e-mail. It works fine when there is a URL with normal characters.

The problem is the page (php) uses ampersands in the URL, and it kills the creation of the link once it hits the first & in the string
(http://www.domain.com/index.php?&direction=0&order=&directory=hank/toy) ends up getting written to the e-mail as:

The script is below. What can I add to get the ampersand problem fixed so the complete URL gets picked up by the script and transfered to the e-mail as intended?

<!-- Begin E-Mail-This-Page Script //-->
<form name="eMailer">
Enter recipient's e-mail:
<input type="text" name="address" size="25">
<input type="button" value="Send this URL" onClick="mailThisUrl();">

var good;
function checkEmailAddress(field) {
// the following expression must be all on one line...
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
if (goodEmail){
good = true
} else {
alert('Please enter a valid e-mail address.')
good = false

u = window.location;
m = "I thought this might interest you...";
function mailThisUrl(){
good = false
if (good){
// the following expression must be all on one line...
window.location = "mailto:"+document.eMailer.address.value+"?subject="+m+"&body="+document.title+" "+u;
<!-- End E-Mail-This-Page Script //-->

06-14-2004, 04:46 AM
put &amp; where every amperstand is (sorry dont no how to spell "and" lol)

06-14-2004, 04:47 AM
You should escape the parameters so that they are URL encoded.

window.location = "mailto:"+document.eMailer.address.value+"?subject="+escape(m)+"&body="+escape(document.title + " " + u);

06-14-2004, 05:23 AM
Perfect...completely handles the problem..
Thanks alot..

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum