Here are some strings that I'm using to ultimately form a HTML mailto link. I'm doing this in javascript. If I output the mailtoString to an alert() I get the link looks just fine. However, when I put it into the location.href the string is cut short at the "&" character. How do I tell the location.href that the "&" is not the end of the mailto link?
var subject = escape('subject');
var body = escape('body');
var reportUrl = document.URL + "/GetUpdatedTableResults?beginDate=" + beginDate + "&endDate=" + endDate + "&fileId=" + DocId + '&languageCode=' + LangCode + '&documentResultType=' + result + '&result=' + ReportedIssue;
var excelUrl = document.URL + 'CurReport/GetCSVReport?beginDate=' + beginDate + '&endDate=' + endDate + '&fileId=' + DocId + '&languageCode=' + LangCode + '&documentResultType=' + result + '&result=' + ReportedIssue;
var mailtoString = 'mailto:?subject=' + subject + '&body=' + body + '%0A%0AWeb:%0A' + reportUrl + '%0A%0AExcel:%0A' + excelUrl;
location.href = mailtoString;
After running the code above I get the following output.
http://localhost:5050/CurReport/GetUpdatedTableResults?beginDate=0
Because immediately after mailto: should be the email address. ? is a valid email characters but & is not. Anyway, the & should be escaped to &.
Related
How to get regex with replace method? In my case I've got string which uses char / between.
input:
var string = "cn/" + companyName + "/st/" + state + "/ic/" + incCi + "/pr/" + priority + "/es/" + emplSystem + "/mc/" + mainCategory + "/sc/" + subCategory + "/ty/" + type;
output:
"cn/Nemesis Group/st/2/ic/null/pr/1 - High/es/null/mc/Add/Button/sc/Core/Label/ty/str"
variable mainCategory and subCategory returns string 'Add/Button' and 'Core/Label'
How to replace 'Add/Button' to 'Add%2FButton' and 'Core/Label' to 'Core%2FLabel' without changing any other char?
string.replace("\/", "%2F")
will change all char / to %2F
You can use encodeURIComponent() and decodeURIComponent() to transform this String
Example:
const companyName = "Company",
state = "State",
incCi = "IncCi",
priority = "Priority",
emplSystem = "EmplSystem",
mainCategory = 'Add/Button',
subCategory = 'Core/Label',
type = "Type";
var string = "cn/" + companyName + "/st/" + state + "/ic/" + incCi + "/pr/" + priority + "/es/" + emplSystem +
"/mc/" + encodeURIComponent(mainCategory) +
"/sc/" + encodeURIComponent(subCategory) + "/ty/" + type;
console.log(string)
It sounds to me like you are looking to encode the url. You can use encodeURI in JS to encode a url.
let encodedURL = encodeURI(url);
You can read more about it here.
If you want to encode the string altogether without ignoring any domain related parts, you can us encodeURIComponent()
let encodedURL = encodeURIComponent(url);
You can read more about their differences here.
EDIT:
If you are not encoding a url and you just want to repalce / with %2F only in mainCategory and subCategory then you need to run the regex on the string itself before joining them.
var string = "cn/" + companyName +
"/st/" + state +
"/ic/" + incCi +
"/pr/" + priority +
"/es/" + emplSystem +
"/mc/" + mainCategory.replace("\/", "%2F") +
"/sc/" + subCategory.replace("\/", "%2F") +
"/ty/" + type;
I am working on twitter integration. I am using CryptoJs to create a signature. Here is the code.
The issue is, following code is working without "in_reply_to_status_id" parameter, but with that parameter it is giving "could not authenticate issue".
var autSign =
'oauth_consumer_key=' + percentEncode(consumer_key) + '&' +
'oauth_nonce=' + percentEncode(nonce) + '&' +
'oauth_signature_method=HMAC-SHA1&' +
'oauth_timestamp=' + timeStamp + '&' +
'oauth_token=' + percentEncode(access_token) + '&' +
'oauth_version=1.0&status=test3&in_reply_to_status_id=1217119832744898564';
var baseString =
"POST&" +
percentEncode(endpoint) + "&" + percentEncode(autSign);
var signingKey = this.percentEncode(consumer_secret) +
"&" + this.percentEncode(access_token_secret);
var encrypted = CryptoJS.HmacSHA1(baseString, signingKey);
var signature = CryptoJS.enc.Base64.stringify(encrypted);
All the \n are removed from the string when the text is put inside mail body .
var valuess = Object.entries(feedBackText);
valuess.forEach(function (key) {
responseText = responseText.concat(' ' + key[0] + ':' + key[1] + '\n');
});
var parsedString = responseText.toString();
window.location = "mailto:myid#gmail.com"+"?subject="+subjectmail+"&body=" +
parsedString;
The following demonstrates how you can solve this using the built-in encodeURIComponent function:
var parsedString = "text on the" + "\n" + "next line";
var link = "mailto:myid#gmail.com" + "?subject=Example&body=" +
encodeURIComponent(parsedString);
console.log(link);
I am trying to create a mailto button, where it creates the title and body of an email.
My code does its job correctly but I realised if the letter '&' is in the text, then the rest of the code is skipped. so the string is visible until '&', and anything after that will not be in the email body.
Have a look a the example: jsFiddle
var title = "my track title";
var artist= "ArtistA & Artist B";
var trackURL= " http://localhost/music";
var subjectText = title + " by " + artist;
var bodyText = "Check out the track " + title + " by " + artist + " on " + trackURL;
var url = 'mailto:' + '' + '?subject=' + subjectText + '&body=' + bodyText;
$(".email").on("click tap", function(event) {
event.preventDefault();
window.location = url;
});
console.log(url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="email">Send Email</button>
In the console it is shown correctly but if you pres the button and open the it in an email application you see that the text is like: Check out the track my track title by ArtistA and the rest is missing.
If I change '&' to 'and' it works correctly and shows: Check out the track my track title by ArtistA and Artist B on http:/localhost/music
Any idea how to fix it? i tried .toString() but did not work.
That is because & is the separator character of the url parameters.
You need to encode you values (encodeURIComponent for the texts and encodeURI for the url) before adding them to the url string.
var title ="my track title";
var artist= "ArtistA & Artist B";
var trackURL= " http://localhost/music";
var subjectText = encodeURIComponent(title + " by " + artist);
var bodyText = encodeURIComponent("Check out the track " + title + " by " + artist + " on ") + encodeURI(trackURL);
var url = 'mailto:' + '' + '?subject=' + subjectText + '&body=' + bodyText;
$(".email").on("click tap", function(event) {
event.preventDefault();
window.location = url;
});
console.log(url);
I have js code that opens a mailto dialog when pressing on link, it is working as "share with a friend" function:
setTimeout( function(){
var subject, body, email_string;
subject = oScript_x.post_title;
body = "you got mail from";
//body += "link: " + "<a href='" + location.href + "'>" + location.href + " </a>";
body += "link " + location.href;
email_string = "mailto:?subject=" + subject + "&body=" + body;
email_string = email_string.replace(/ /g, "%20" ).replace(/\n/g, "%0A");
I tried to use this :
body += "link: " + "" + location.href + " ";
But no luck...
So now I am only showing the link as text with no link.
I would appreciate help to have the link clickable and under an anchor text.
Thanks
The problem is that you are not escaping things properly. Instead of manually replacing certain patterns, you should use encodeURIComponent for each of the parameters that you add.
In other words, your code should look like this:
var subject = ... // whatever you do to create this string
var body = ... // whatever you do to create this string
var encodedSubject = encodeURIComponent(subject);
var encodedBody = encodeURIComponent(body);
var emailLink = 'mailto:?subject=' + encodedSubject + '&body=' + encodedBody;
// ... use emailLink