why does jQuery skip the code if '&' is in the string - javascript

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

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

AJAX search - no results returns last line

I am trying to implement a search box that will search a particular play (AJAX file) for any instances of the word, if the word is found it then outputs that line. My problem is that if no results are found of the word instead of continuing to show the entire play it outputs the last line of the play only.
My function:
function searchResults(query) {
var temp = "\\b" + query + "\\b";
var regex_query = new RegExp(temp, "gi");
var currentLine;
var num_matching_lines = 0;
$("#mainOutput").empty();
$("LINE", current_play_dom).each(function () {
var line = this;
currentLine = $(this).text();
matchesLine = currentLine.replace(regex_query, '<span class="query_match">' + query + '</span>');
if ( currentLine.search(regex_query) > 0 ) {
num_matching_lines++
$("#mainOutput").append("<br /><p class='speaker_match'>"+ $(line).parent().find('SPEAKER').text() +"</p>");
$("#mainOutput").append("<p class='act_match'>"+ $(line).parent().parent().parent().children(':first-child').text()+"</p>");
$("#mainOutput").append("<p class='scene_match'>"+ $(line).parent().parent().children(':first-child').text() +"</p>");
$("#mainOutput").append("<p>" + matchesLine + "</p>");
$("#mainOutput").append("<br>");
}
});
$("#mainOutput").append("<p>" + matchesLine + "</p>");
$("#sideInfo").append("<p>Found " + query + " in " + num_matching_lines + " lines</p>");
}
Also as a side question is there a neater way to do this:
$(line).parent().parent().parent().children(':first-child')

dynamics crm 4.0 lookup field onload() query

I have an account entity with a lookup field schoolLookupId which refers back to a custom entity new_schools. The lookup field only display the name of the school. What I would like to be able to do using the onload() event handler of the account form is to run some javascript code that will query the new_phonenumber attribute of the new_schools entity to see if it matches a value i provide lets say var x = "1234" and if it does then update schoolLookupId accordingly with the name of the school that corresponds with the found phone number. i.e update the lookup field with a phone number that already exists without creating a completely new lookup value.
I can get the attributes of the lookupfield using
var name = crmForm.all.schoolLookupid.DataValue[0].name
var id = crmForm.all.schoolLookupid.DataValue[0].id
var typename = crmForm.all.schoolLookupid.DataValue[0].typename
but I can't figure out how to retrieve, compare the data that lies behind the lookup field, and update the lookupfield accordingly.
Your help as always is invaluable.
Try put this code in load event:
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>new_schools</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>new_schoolsid</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" <q1:Criteria>" +
" <q1:FilterOperator>And</q1:FilterOperator>" +
" <q1:Conditions>" +
" <q1:Condition>" +
" <q1:AttributeName>new_phonenumber</q1:AttributeName>" +
" <q1:Operator>Equal</q1:Operator>" +
" <q1:Values>" +
" <q1:Value xsi:type=\"xsd:string\">"+crmForm.all.new_phonenumber.DataValue+"</q1:Value>" +
" </q1:Values>" +
" </q1:Condition>" +
" </q1:Conditions>" +
" </q1:Criteria>" +
" </query>" +
" </RetrieveMultiple>" +
" </soap:Body>" +
"</soap:Envelope>" +
"";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "http://"+window.location.hostname+":"+window.location.port+"/mscrmservices/2007/crmservice.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction"," http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseXML;
if (_resultXml.xml.search('<q1:new_schoolsid')>0)
{
var val = xmlDoc.getElementsByTagName("q1:new_schoolsid")[0].childNodes[0].nodeValue;
var lookupitem = new Array();
lookupitem[0] = new LookupControlItem(val , typecode, name);
crmForm.all.schoolLookupid.DataValue = lookupitem ;
}
}
I don't try this code be careful. Use this code as a guide.
Hope this helps.
If i answered your question, please mark the response as an answer and also vote as helpful.

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