How can I do this:
var newContactEmail = "abc#cc.com";
document.getElementById('contactEmail').innerHTML = '' + newContactEmail + '';
When I click on the mail link, it opens but the "To" field is blank.
You are putting the closing double quotation mark too soon.
var newContactEmail = "abc#cc.com";
document.getElementById('contactEmail').innerHTML = '' + newContactEmail + '';
Instead of href="mailto:"' + newContactEmail + '" ' try href="mailto:' + newContactEmail + '" '.. Else it would look like href = "mailto:"someemail#a.com .
Related
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 am trying to make a dynamic HTML which I'll append using jquery before append if I'll console the HTML everything looks fine but after append in browser The whole structure messed up.
Here is my HTML :
<script>
var title = "my title";
var toolbar ='<hm_toolbar user="current_user " upp="hm" index="hmIndex "></hm><synapse_toolbar uuid=" hm.hm_pk " my-method="get_linked_facts" ng-if=" flag == true "></synapse_toolbar>';
var map_html = '<a onclick="panToMarker(' + lat + ',' + long + ')"> ' + title +'"</a>' + toolbar ;
var li_html = "$('#" + title + "').append('<li class=\"list-group-item\"><div dynamic=\" " + map_html + " \"></div></li> ')" ;
var g =title.replace(/ /g,"_");;
var fn = "function "+ g +"(){ console.log('--working--'); "+ li_html +"; }";
console.log(fn)
eval(fn);
}
</script>
When the above li_HTML does its work means append the HTML the appended is all messed up
Appended HTML :
<li class="list-group-item"><div pantomarker(49.711083,6.251445)"="" dynamic=" <a onclick="> Test"<hm_toolbar index="hmIndex " upp="hm" user="current_user "></hm_toolbar><synapse_toolbar ng-if=" flag == true " my-method="get_linked_facts" uuid=" hm.hm_pk "></synapse_toolbar> "></div></li>
I know I have messed up the concatenation using with quotes but I am not able to fixed the bug .
You said that the console.log() looks OK but you have eval() after that which means that you have to escape some quotes twice.
Try this:
var toolbar ='<hm_toolbar user=\\\'current_user \\\' upp=\\\'hm\\\' index=\\\'hmIndex \\\'></hm><synapse_toolbar uuid=\\\' hm.hm_pk \\\' my-method=\\\'get_linked_facts\\\' ng-if=\\\' flag == true \\\'></synapse_toolbar>';
var map_html = '<a onclick=\\\'panToMarker(' + lat + ',' + long + ')\\\'> ' + title +'</a>' + toolbar ;
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
I have this little problem.
I have this javascript in my html page:
function myfunction(form)
{
var name = document.details.txtName.value + "\n";
var street = document.details.txtStreet.value + "\n";
var city = document.details.txtCity.value + "\n";
var stateProvince = document.details.txtStateProvince.value + "\n";
var postalCode = document.details.txtPostalCode.value + "\n";
var country = document.details.txtCountry.value + "\n";
var homeTel = document.details.txtHomeTel.value + "\n";
var workTel = document.details.txtWorkTel.value + "\n";
var fax = document.details.txtFax.value + "\n";
var mobile = document.details.txtMobile.value + "\n";
var email = document.details.txtEmail.value + "\n";
var enquiry = document.details.txtEnquiry.value + "\n";
var message = (name +
street +
city +
stateProvince +
postalCode +
country +
homeTel +
workTel +
fax +
mobile +
email +
enquiry);
alert(message);
location='mailto:somecrazyassemail#gmail.com?subject=Message From Redec Website&body=' + message;
return false; //So that the page can stay at its current location.
In the messagebox that pops up, it displays the strings underneath each other, which I want.
but when this opens outlook it is all in one long string. how can I fix this?
}
The mailto is particular attribute. You have to encode the string using escape function.
But for the new lines, you can use %0D%0A.
See this site for more informations.
You're inserting the text as HTML, so new line characters '\n' are simply parsed as 'n'. To insert a line break, using the corresponding HTML element <br>.
For example:
var name = document.details.txtName.value + "<br>";
// ^^^^
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 &.