Hola! ¡Tenemos nuestra Comunidad en Español!

How to use a variable in a script with mailto link

SOLVE
New Contributor

I have a script that works great for generating links for my social media buttons. 

What I'm having trouble with is generating a link for a mailto button. I'd like to include the Blog Post Title in the email subject field and the URL of the blog post in the Body of the email. 

 

My script is below. 

 

<script>
$( document ).ready(function() {
var Url = window.location.href;
var fbLink = "https://www.facebook.com/sharer/sharer.php?u=" + Url;
var twhash = "hashtags=DataLiteracy";
var twlink = "https://twitter.com/intent/tweet?text=Check Out This Article: " + Url + "&" + twhash;
var lilink = "https://www.linkedin.com/sharing/share-offsite/?url=" + Url;
var m2link = "mailto:?subject=";
document.getElementById('fbQuote').href = fbLink;
document.getElementById('tweetQuote').href = twlink;
document.getElementById('linkedin').href = lilink;
document.getElementById('mailto').href = m2link;
});
</script>
Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
HubSpot Employee

Hey @jspencer you could try pulling the value of the document title and setting that as a variable to pull in the blog post title as the subject in your mailto link. So essentially you'd just add in: 

 

var postTitle = document.title.split(' ').join('%20');

You'd need to replace all spaces in that variable with '%20' for the mailto link. Then you could change your mailto link to something like this: 

 

var m2link = "mailto:?subject=" + postTitle + "&body=" + Url;
1 Reply
Highlighted
HubSpot Employee

Hey @jspencer you could try pulling the value of the document title and setting that as a variable to pull in the blog post title as the subject in your mailto link. So essentially you'd just add in: 

 

var postTitle = document.title.split(' ').join('%20');

You'd need to replace all spaces in that variable with '%20' for the mailto link. Then you could change your mailto link to something like this: 

 

var m2link = "mailto:?subject=" + postTitle + "&body=" + Url;