简体   繁体   中英

Anchor tag with mailto and javascript function

Below is my piece of code, I am trying to invoke a javascript function and mailto when user clicks a anchor-link, my javascript function is throwing errors, if I remove mailto it works fine. Please advise if I am missing anything here.

    <a rel="nofollow" onclick="javascript:confirm('2R','ADDRESS');return true;" href="mailto:housing@test.edu?
subject=Incorrect Residential Address">housing@test.edu (Click Here)</a>

here is my confirm function which has ajax call

    function confirm(addcode,type){

     $.ajax({
            url: 'https://mysite.url/confirm',
            dataType: 'json',
            method: 'POST',
            cache : false,
             beforeSend: function setHeader(xhr){
                 console.log(addcode + '<--->'+type+'<--->'+term);
                $("#main-content").mask("Please wait...");
            },
            data: {
                'addCode': addcode,
                'type': type,
                'termCode' : term
            },
            success: function(data){
                notify("Confirmed");
                $("#main-content").unmask(); 
            },
            error: function(xhr, testStatus, error) {
                $("#main-content").unmask(); 
                notify("Unable to Confirm");

                console.log(error);
                console.log(xhr);
                console.log(testStatus);
            }
        });
}

Can't comment yet: I think it isn't a real 'error', more that the code doesn't work as he expects it to.

  • you want a confirm box before you activate 'mailto:'.
  • if user presses yes, then it will activate it.
  • if the user presses no, you want it to do nothing.

Here a crossbrowser solution: jsfiddle: http://jsfiddle.net/kychan/9KuLM/

function mConfirm(e, a)
{
    if (confirm('Are you sure you want to send it to: ' + a + '?'))
    {
        return true;
    }
    else
        e.preventDefault();
}

edit:

btw. you can't change the title of a confirm box. The 'confirm()' function only works with 1 argument. If you insist on making a confirm box with a title you should use a third-party script or your own function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM