Reputation: 191779
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 protected]";
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:[email protected]";
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?
Upvotes: 1
Views: 1046
Reputation: 2166
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 protected]";
};
});
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 protected]";
});
Upvotes: 2