I have a C# ASP.NET app that creates a JavaScript array of values for some user profile information. Client-side, I use jQuery/JavaScript to read the array and generate a mailto link. Some of the fields can contain special characters, such as ' & = / \ ".
Here's the C# code:
private String generateElementsArray(){
StringBuilder sb = new StringBuilder();
sb.Append("var currentElements = { currentUserName: '");
sb.Append(currentUserName);
sb.Append("', currentUserEmail: '");
sb.Append(currentUserEmail);
sb.Append("', currentSite: '");
sb.Append(currentSite);
sb.Append("', currentTitle: '");
sb.Append(currentTitle);
sb.Append("'}");
return sb.ToString();
}
I write the value of the above method to the page, which produces this JavaScript:
<script type="text/javascript">var currentElements = { currentUserName: 'Alex', currentUserEmail: 'myemailID', currentSite: 'Helpdesk', currentTitle: 'Phone User Guides & Troubleshooting'}</script>
Then I generate the email link using this JavaScript code, attaching the anchor tag to an element on the page:
function generateEmailTo(){
var body = currentElements.currentUserName + ' has shared a page with you on the intranet.%0A%0APage Title: %22' +
currentElements.currentTitle.replace("&","and") + '%22%0A' + $(location).attr('href').replace('#','');
var subject = currentElements.currentUserName + ' has shared a page with you on the intranet';
var mailto = 'mailto: ?body=' + body + '&subject=' + subject;
var anchor = '';
$("#email-placeholder").wrap(anchor);
}
....
<img id="email-placeholder" title="Send this page to a friend." src="<%= baseUrl %>/SiteAssets/media/icons/email-icon.gif"/>
For the mailto body text, I've noticed that it only takes a small set of the encoded characters, such as %22 for double-quotes, and %0A for line breaks. How do I pass the special characters such as single quotes, ampersands, etc., to the mailto body text and keep JavaScript happy?
Mailto is a URI scheme so all of its components must be URI encoded. You can use JavaScript encodeURIComponent function to encode mailto components. Please see the following example:
function buildMailTo(address, subject, body) {
var strMail = 'mailto:' + encodeURIComponent(address)
+ '?subject=' + encodeURIComponent(subject)
+ '&body=' + encodeURIComponent(body);
return strMail;
}
var strTest = buildMailTo('abc#xyz.com', 'Foo&foo', 'Bar\nBar');
/* strTest should be "mailto:abc%40xyz.com?subject=Foo%26foo&body=Bar%0ABar" */
window.open(strTest);
Hope this help.
Related
I have developed the javascript email form, which was discussed here:
HTML Assigning the checkbox to the form action already defined
and everything works fine, also with jQuery elements.
The problem unfortunately lies in the textarea, where the text input seems to be limited for no reason.
According to the word counter here:
https://textool.io/character-counter/
I can work only with 220 characters. If I exceed this limit, the mailto: fails.
When I put i.e. one sentence, everything is alright. The problem emerges when I cross some unknown limit of characters. Then the email is not populated.
The full code is here:
https://jsfiddle.net/pq63e4yr/
and the situation depicted here:
I have "discovered" it by using the lorem ipsum text from here:
https://www.lipsum.com/
and now I am trying to find where the problem might be. I saw, that MS Edge has the similar problem
FormData constructor loses textarea value in Edge
but it looks like the Chrome too...
The sample of code (apart from the link above) looks as this:
document.getElementById("cfsubmit").addEventListener("click", function() {
var check = document.getElementById("cfemail");
var formEl = document.forms.cityfibre_form;
var formData = new FormData(formEl);
var jobAddress = formData.get('address');
var jobPostcode = formData.get('postcode');
var surveyorName = formData.get('surveyor');
var feedback = formData.get('feedback');
var subject = jobAddress + ", " + jobPostcode + " - site survey submission from " + surveyorName;
var body = 'SURVEYOR: ' + surveyorName + '\n' +
'ADDRESS: ' + jobAddress + ' ; POSTCODE: ' + jobPostcode + '\n' +
'FEEDBACK: ' + feedback;
var mailTo = "mailto:mk#gmail.com?subject=" + encodeURI(subject) + "&body=" + encodeURI(body);
if (check.checked == true) { // If checked then fire
let link = document.createElement("a");
link.href = mailTo;
link.click();
link.remove();
}
mainForm.submit();
});
<figure class="feedback">
<label class="feedtitle" for="Message">Leave
feedback</label>
<textarea id="opfeedback" name="feedback"></textarea>
<br>
<div class="emailreceipt">
<input type="checkbox" id="cfemail" name="email">
<label class="checking" for="cfemail">Send me an email
receipt of my responses</label>
</div>
</figure>
and I have no idea what's going on here. Is the formData.get attribute limited with characters?
The similar question:
Getting around mailto / href / url character limit
says about the 2083 characters limit, since according to this counter: https://textool.io/character-counter/
I can work only for 220 characters. I use the Chrome browser.
Is there some reason behind it?
I am trying to grab some text content (HTML comments) on the page and convert it into variables. The text content will be of the format:
I have constructed some Javascript that works up to a point, but will not go past the final variable declaration "var shorturl = ..."
Javascript code below:
$(document).ready(function() {
if (document.getElementById("ajax-gallery")) {
var shortcode = '<!-- [var module="gallery"; var id="1"; var type="single"; var data="only";] -->';
shortcode = shortcode.replace("<!-- [", "");
shortcode = shortcode.replace("] -->", "");
eval('shortcode');
var shorturl = "shorturl = " + module + "/shortcode_" + type + ".php?id=" + id + "&data=" + data;
eval('shorturl')
updateGallery(shorturl, "ajax-gallery");
}
});
I would have thought this would work, but apparently not. I know that eval is frowned upon but at present I cannot find a "nicer" method.
[![enter image description here][1]][1]In href how to pass path with dynamic data, below I'm giving my code:
var abc = response[i].DocumentName;
var photoName = "<a href='#Url.Content("~/UploadImage/")" + abc +'" target="_blank" >'+response[i].DocumentName+'</a>';
in debugger mode i am getting like this:-
photoName = "jpeg2_10514.jpg"
which is not working for me
Try this:
var photoName = "" + response[i].DocumentName + "";
In Javascript you have to escape doublequotes " with a backslash \ if you want them to appear in the string.
The backslash in + abc + "\" is there to escape the second " to enclose the href in doublequotes.
EDIT
I added the missing doublequote befor the anchor tag according to the tip of karan.
I'm building an app that uses a webview. It loads mobile version of a website. But my client wanted me to change and delete some text.
I did it with javascript but after i did this, some sliders stopped working anymore. Without javascript it works perfect. But i have to change and remove texts somehow.
my webview settings;
wSettings=webView.getSettings();
wSettings.setJavaScriptEnabled(true);
wSettings.setDomStorageEnabled(true);
webView.loadUrl("http://www.evdusum.com");
and the javascript that i used in onPageFinished;
#Override
public void onPageFinished(WebView view, String url) {
String link1 = "SİTEDE ARAYIN";
String link2 = "ARA";
String a="Bu sitede kullanılan yazılı ya da görsel dokümanlar izinsiz kullanılamaz.";
String b=" ";
view.loadUrl("javascript:(function(){document.body.innerHTML = document.body.innerHTML.replace('" + link1+"', '" + link2+"')})()");
view.loadUrl("javascript:(function(){document.body.innerHTML = document.body.innerHTML.replace('" + a+"', '" + b+"')})()");
((RelativeLayout)findViewById(R.id.start)).setVisibility(View.GONE);
}
Until Android api 18 (JELLY_BEAN_MR1) you can use this view.loadUrl("javascript:");. Just use ONE call to insert and call your js. If you need to perfome long code, build a function and call it at the "same call" of webView.loadUrl().
Some example:
webView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.innerHTML = function your_function(var1, var2){document.body.innerHTML = document.body.innerHTML.replace(var1, var2); return true;}" +
"parent.appendChild(script);" +
"your_function('"+link1+"','"+link2+"')" + ";})();");
Android api >=19 use webView.evaluateJavascript. Here some example:
webView.evaluateJavascript("(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.innerHTML = function your_function(var1, var2){document.body.innerHTML = document.body.innerHTML.replace(var1, var2); return true;}" +
"parent.appendChild(script);" +
"return your_function('"+link1+"','"+link2+"');})();", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
Log.v("TAG", "Response from JS"+value)
}
});
I'm trying to get the url parameter(device) and call it out in the body. the code below works great except it'll come out as iPhone+4s*. Is there any way to remove the + and * and just have it say iPhone 4s?
url - http://mydomain.com/click.php?device=[[device_name]]
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
);
}
</script>
<body>
<script>document.write(getURLParameter('device'))</script>
</body>
You have to just replace all & + = because they aren't going to decode as they are allowed characters in url-encoding.
uri = decodeURIComponent(uri);
uri = uri.replace(/\+/g, '-');