Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ampersand trouble in JavaScript

    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 //-->
    Last edited by aram; 06-14-2004 at 03:47 AM.

  • #2
    New Coder
    Join Date
    May 2004
    Location
    Some place called the Earth I think...
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    put &amp; where every amperstand is (sorry dont no how to spell "and" lol)
    Dont do drugs, get high on life

    13 years and getting nowhere fast.....


    đ­˘M_M÷˝kŔą

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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);

  • #4
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect...completely handles the problem..
    Thanks alot..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •