So I have this hidden form and this ‘I like this’ button that I would like to insert into emails I send out.
<form method="post" action="http://someWebsite.com/he_likes_it">
<input type="hidden" name="who_liked" value="heLikedIt@something.com" />
<input type="submit" value="I Like This" />
</form>
I can insert the button into my email just fine. So, whenever an email recipient clicks on the button in their email, a POST request with the form data is sent to my server letting me know who liked my email. However, clicking on the button also opens up the link (new browser window/tab, etc.) directing the users to my site. How do I prevent the clicking of the button from opening the link and only just send the data in the form to my server?
I want the like button to be unobtrusive. I don’t want it annoying the email recipient by having a new tab open up. I know its possible with javascript, however, I’m pretty sure most email service provider escape javascript in emails as they should.
For starters, completely forget about javascript — it isn’t going to happen. No email client would allow JS through, it is simply too risky.
As for the form method, putting aside whether the email client lets it through, if the page immediately closed itself (see snippet below), some operating systems, browsers, and email clients may play along and “give” the focus back to the mail client (which is what you’re after — the user is brought “back to where they were”). However, there’s no guarantee on how the client OS, browser, and email client will react.
Further, some people use non-HTML clients, and some clients (like Outlook) will probably disallow this completely. From a security perspective, this has potential for exploit, so even if what you are doing is not exploitative, the entire practice has that potential and thus may be blocked.
Non-HTML email clients may dump your HTML as text, so this will look pretty bad in that scenario.
To close a window immediately, you can try:
So the page at
http://someWebsite.com/he_likes_itconsists of only this as output (after doing whatever server-side stuff needs to be done). Depending on the browser, and whether other tabs are open, and a few other factors, the window might just close without any problem. Might is the operative word! Then the user might be taken back to the email client. There’s no way to script this, no way to control it.As an alternative, I suggest you do something less prone to problems and the vagaries of email clients, OSes, and browsers… and that is far less “sneaky” — don’t conceal what you’re doing from the user. Instead of a form, simply include a link in your email:
The unique identifier could be the email (which is what you were doing in your form anyway), or some kind of token that is linked to the user. In either case, on the page you can include some text to thank the user for their feedback:
This is cross-browser, cross-OS, and will work for any HTML-enabled email client. For plain-text clients, they’ll be able to copy-paste the URL into their browser without seeing an overt amount of markup clutter. Your users will appreciate your transparency, your expressed gratitude, and that you’re not sending them emails that set off their email client’s security warnings.