Foreign letters failing when sending emails - javascript

I sometimes have to send emails in a German and I need to use ö ä ß etc... I have text written containing these letter and using alert() they appear just fine. I have code to send an email :
var link = "mailto:" + SendTo
+ "&cc= "
+ "&subject=" + escape(subjectLine)
+ "&body=" + escape(BodyText);
window.location.href = link;
When I click a button to send the email, the text is missing these foreign letters e.g gruß comes out as gru. Do I need to put anything in here to make sure these letter don't disappear?
Thank you in advance

The following articles shows how to decode the letters using javascript :
Javascript decoding html entities
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
HTML Entity Decode
Using jquery:
varTitle = $('<textarea />').html("Chris&apos; corner").text();

Related

why Whatsapp web api not sending &?

I'm using this Javascript to send some information to someone on whatsapp but don't know why Javascript is not sending my message after using & I write it after company name. it is very necessary to use this in middle of the text. what should I do?
I want to use & after conpnayName so I can deliver what I want to the my client.
function whatsapp(){
var compnayName = $("input[name=companyName]").val();
var p_number = $("input[name=p_number]").val();
console.log(s_no)
window.open( "https://api.whatsapp.com/send?phone=+91"+ p_number +"&text=" +"Download Your Bill from : http://localhost/learn/php_invoice/search-bill.php?order_id="+ compnayName + "&" + "p_number" + p_number ,'_blank');
}
You should url encode the "&" for that specific symbol.
Take a look at url encoding https://www.w3schools.com/tags/ref_urlencode.asp
Try something like,
window.open( "https://api.whatsapp.com/send?phone=+91"+ p_number +"&text=" +"Download Your Bill from : http://localhost/learn/php_invoice/search-bill.php?order_id="+ compnayName + "%26" + "p_number" + p_number ,'_blank');
Here "%26" refers to "&".

Replace String Text with HTML

I'm trying to implement emoticons so for example :happy: should display an emoticon. However, the text is sanitized and does not generate the actual html.
How can I replace certain strings such as :happy: with html image?
Current attempt (replace :happy: with html):
var data = snapshot.val();
var username = data.name || "anonymous";
var message = data.text;
// REPLACE STRING WITH IMAGE
message = message.replace(":happy:", "<img src='https://s3-us-west-1.amazonaws.com/static/emoticons/1.0.jpg'>");
//CREATE ELEMENTS MESSAGE & SANITIZE TEXT
var messageElement = $("<li>");
nameElement.text(username);
messageElement.text(": "+message).prepend(nameElement);
//DISPLAY MESSAGE
messageList.append(messageElement)
Update:
I think something like text.splitText() should be used here. But I'm trying to find the best way to find the text, then split it.
Use .html() to have an element effect. .text() will just put the content without having any HTML effect
messageElement.html(": "+message).prepend(nameElement);
You can use a single detached DOM element to sanitize the inputs, then convert strings to images.
Done by setting Node.textContent[mdn] and getting Element.innerHTML[mdn] on the detached node.
Then using String.prototype.replace()[mdn] method on the resulting string.
This is library free.
var username = "anonymous",
message = "Some text with an happy <b>emoticon</b> :happy: and a sad <i>emoticon</i> :sad:.",
messageList = document.getElementById("messageList"),
sanitizer = document.createElement("p");
//CREATE ELEMENTS MESSAGE
var messageElement = document.createElement("li");
//SANITIZE TEXT
sanitizer.textContent = message;
message = sanitizer.innerHTML;
//REPLACE STRING WITH IMAGE
message = message.replace(":happy:", "<img src='https://s3-us-west-1.amazonaws.com/horde.tv/static/emoticons/1.0.jpg'>");
message = message.replace(":sad:", "<img src='https://static-cdn.jtvnw.net/emoticons/v1/86/1.0'>");
messageElement.innerHTML = username + ": " + message;
//DISPLAY MESSAGE
messageList.appendChild(messageElement);
<ul id="messageList"></ul>

Create button to get value from input and open default email client?

I have a button in a form that I want to collect the value (which is an email address) from a nearby input and construct an email using the default email client. It would be a similar function to your standard email link:
<a href="mailto:someone#example.com?Subject=Test" target="_blank">
Only in my instance, it's a button constructing the email and it also collects a value. What I have so far:
HTML
<input class="an-input" type="text">
<button class="ui-state-default inputButton an-button">SEND</button>
JS:
var ACEbutton = conf._input.find('.an-button');
ACEbutton.click(function() {
var inputValue = conf._input.find('input').val();
var subject = "This is a subject";
var subjectEncoded = encodeURIComponent(subject);
document.getElementById('emailMe').href = "mailto:'+inputValue+'?subject=" + subjectEncoded;
} );
The code as it stands isn't opening the email.
You should end the string statement before concatenating it. If you start a string with double quotes, you should end it with double quotes. Same goes for single quotes.
document.getElementById('emailMe').href = "mailto:"+inputValue+"?subject=" + subjectEncoded;
instead of
document.getElementById('emailMe').href = "mailto:'+inputValue+'?subject=" + subjectEncoded;
//fix syntax like Ricardo said
document.getElementById('emailMe').href = "mailto:"+inputValue+"?subject=" + subjectEncoded;
//trigger email client
document.getElementById("emailMe").click();

jquery - append html doesn't append the empty String inside the div

I am doing a chat project in java.
Using my Server response through jquery-ajax , I am creating an html and append the html to div.
My jquery - html alert without empty string-name(note the id='user' in the alert) will be ,
after appending this in the chat area ,
After jquery-html alert with empty string-name(note the id='user' in the alert) will be ,
after appending this in the chat area ,
My jquery for creating the HTML will be ,
function send(name , message , time){
//name = $("#name").val();
//message = $("#message").val();
var user = "<div id='user' class='fontStyle'>"+name+"</div>";
var msg = "<div id='msg' class='fontStyle'>"+message+"</div>";
var timeStamp = "<div id='time' class='fontStyle'>"+time+"</div>";
var row = "<div id='msgSet' >"+ user + msg + timeStamp +"</div>";
alert(row);
return row;
}
There is no empty string will be present in chat area.
Hope our stack members will help me.
You can escape html your string so it keeps the white spaces. See details here.
Or you can trim your message string (=remove spaces at the beginning and end of it) with such an instruction, and then manualy add the spaces (with the code inside a div, span, ...):
s.replace(/\s+/g, ' ');

How to include javascript generated text into html link?

I have a javascript that displays a generated text into a div:
document.getElementById('phrase').innerHTML = phrase;
PHRASE_TEXT_GETS_SHOWN_HERE
Basically I'm trying to set up a link that will take the text and post it to twitter with a link:
Clicky for tweety
How can I include the generated text in the link?
Do you mean like:
function setPhrase(phrase) {
var url = 'http://twitter.com/home?status=' + encode(phrase);
$('#phrase').html('' + phrase + '');
}
...?
Un-jQuerying it should be straightforward enough, if that's how you roll.
This is un-jQueried:
function setPhrase(phrase) {
var url = 'http://twitter.com/home?status=' + encodeURI(phrase);
document.getElementById('phrase').innerHTML = '' + phrase + '';
}
If you didn't see my failbraining encode for encodeURI as an error you should use Firebug. It may also fail if you have more than one element with the id phrase or if you have no elements with the id phrase.

Categories

Resources