简体   繁体   中英

Simultaneously open mailto and submit form

At management's request, I need to open the user's email client and submit a form on a button click. I initially had

window.location = "mailto:email@example.com";

as the callback to the click event for the submit input, but this doesn't work. It seems like the form submits to quickly.

Using window.open does work, but it creates a blank window that is undesirable.

I also had the idea to prevent and delay the form submission as in

window.location = "mailto:personalcounselor@gleim.com";
setTimeout(function () { 
    $(this).closest('form').trigger('submit');
}.bind(this), 1000); 
e.preventDefault();

This also works, but it seems sketchy to me.

Is there any way to submit the form and open a mailto link at the same time?

I don't know if this will work, but I am sure with a bit of tweaking it should work and have the desired result which you are after (it was too long to fit in a comment, if it will not work I will gladly delete it!);

$("form").on("submit", function() {
     window.onbeforeunload = function() {
         window.location = "mailto:email@example.com";
     };
});

Simply put when the form is submitted, set the onbeforeunload event to change the location to the mailto. I think doing it this way will only make the mailto open if the form is submitted rather than when a user just navigates away.

I don't know if this will work, or how hacky it is, but thought I would throw in my two cents!

UPDATE

On form submit, mailto from javascript with form values

This does seem to work and verified by others.

$("input[type=submit]").click(function(){
     window.location.href = "mailto:email@example.com";
});

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