49

How to include javascript in HTML emails - We need it for expanding and collapsing of the content in the HTML email.

3
  • Collapsing can be achieved using CSS: campaignmonitor.com/blog/email-marketing/2011/08/… That's mainly for iPhones however. Commented Aug 26, 2016 at 14:07
  • Possible duplicate of Is JavaScript supported in an email message? Commented Jan 24, 2017 at 1:58
  • I think, you can make javascript attached in mail with defaulted encoding base64 and then do your code tweaks basically for rendering. Check with concerned RFCs from IETF. There is a way. Commented Nov 29, 2017 at 13:45

8 Answers 8

72

I don't think that is possible in an email, nor should it be. There would be major security ramifications.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree with you. But I got an advertisement mail from our country popular website: pigu.lt and inside mail been placed (and changing) count down timer to discount time is over. HOW IT'S POSSIBLE? Via image exceptions?..
@JDSolutions The timer is a static image, but the server serves new and new one, and it appears dynamic. Search movableink to learn more about dynamic elements in email.
34

Agree completely with Bryan and others.

Instead, consider using multiple sections in your email that you can jump to using links and anchors (the 'a' tag). I think that you can emulate the behavior you want by including multiple copies of the text further down in your email. This is a bet messy though, so you could just have sets of anchors that link to each other and allow you to move back in forth between the 'summary' section and the 'expanded' one.

Example:

<a href="#section1">Jump to section!</a>
<p>A bunch of content</p>
<h2 id="section1">An anchor!</h2>

Clicking on the first link will move focus to the sub-section.

2 Comments

Note that you don't have to use <a name="foo"> to put down an anchor for the link - just having an element with the proper id attribute will work. So if you have <a href="#section1">Jump to section!</a>, then later on when that section start you just need something like <h1 id="section1">Section 1</h1>.
While the others are obviously correct here as well, I'm a bit shocked that this answer didn't get more up-votes for being not only correct, but the only attempt to course-correct the OP.
25

Do not depend on this. Any good mail client will not support executable code within an email. Any knowledgeable user will not use a client that does.

Comments

21

Here's what you CAN do:

You can attach (to the email) an html document that contains javascript.

Then, when the recipient opens the attachment, their web browser will facilitate the dynamic features you've implemented.

9 Comments

Voted up! I want to do this regardless to verify that the client will not run the script.
Those who voted this down, provide your reasons, especially if it was just a knee-jerk "moral" reaction, for this is a technical discussion. Anyone, especially those who got this deep on this page, is clearly aware of the security ramifications anyway. Also, regardless of that, the good guys who read this should know -- even more than the bad guys!... -- what can be done and what can't.
When "something asked for" is "impossible", the best you can do is indicate the closest similar possibility. I stand behind my answer. Unusual isn't impossible, and no one here has offered anything better.
The down-votes on this one are unfair. It's a creative, non-standard work-around. It is perfectly legal, and not a misuse of email attachments or HTML. Any reader can decide for themselves whether this an acceptable workaround or too much like a hack.
I recently proposed an idea that would allow you to attached an entire web application to an email via a single javascript file, without the current need for an HTML Wrapper. See here.
|
12

The short answer is that scripting is unsupported in emails.

This is hardly surprising, given the obvious security risks involved with a script running inside an application that has all that personal information stored in it.

Webmail clients are mostly running the interface in JavaScript and are not keen on your email interfering with that, and desktop client filters often consider JavaScript to be an indicator of spam or phishing emails. Even in the cases where it might run, there really is little benefit to scripting in emails.

Keep your emails as straight HTML and CSS, and avoid the hassle. Here is what you can do in html emails: https://www.campaignmonitor.com/guides/coding/technologies/

Comments

10

Just as a warning, many modern email browsers have JavaScript disabled for incoming emails as it can cause security problems. This means that many of the people you are emailing may not be able to use the content.

PS. Didn't see above post's at time of posting. My bad.

Comments

3

you can use html radio/checkbox input with labels and css to achieve the expanding effects you want.

Comments

2

What you are trying to achieve should be done in the web browser because javascript simply doesn't work with html email design. The various email clients that are out there e.g. gmail, outlook, yahoo strip scripts put of the code for security reasons.

It is best to just use HTML and CSS to style your emails. Maybe you could have a call to action (cta) in your html email that sends the user to a web page with your expanding and collapsing content feature.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.