...

View Full Version : Ampersand trouble in JavaScript



aram
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:
http://www.domain.com/index.php?

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

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.')
field.focus()
field.select()
good = false
}
}

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

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

glenngv
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);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum