HTML concatenation issues in JS - javascript

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 ;

Related

How to keep the next line \n from getting removed inside the email text?

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);

Hyperlink in javascript, for mail subject

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

how do I swap text after comma delimiter?

The following code is currently being generated and produces 'LastName, FirstName'
<div id="welcomeMenuBox">
<spanid="zz4_Menu_t" class="ms-menu-althov ms-welcome-root">
<a id="zz4_Menu" class="ms-core-menu-root" title="Open Menu" href="javascript:;">LastName, FirstName</a>
</span>
</div>
I would like to swap the text on page load so that it says Welcome FirstName, LastName in either JQuery or JavaScript.
You can use:
var text = $('#zz4_Menu').text().split(',');
$('#zz4_Menu').text('Welcome ' + text[1] + ', ' + text[0]);
Fiddle Demo
Here is one method -
var currentText = $('.ms-core-menu-root').text();
var arrayText = currentText.split(',');
var newText = 'Welcome ' + arrayText[1] + ' ' + arrayText[0];
$('.ms-core-menu-root').text(newText);
JavaScript :
var a = document.getElementById('zz4_Menu')
var res = a.innerHTML.split(",")
a.innerHTML = "Welcome " + res[1] + "," + res[0]
Example

Javascript variables email link - "To" field is blank

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 .

Formatting Strings in JavaScript to use as HTML "MAILTO" links

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 &.

Categories