JavaScript mail subject/ message autofill - javascript

I am trying to autofill the subject and message fields in an Outlook email using mailto:. Please, see the JavaScript below. Any suggestions? Thank you in advance. (besides, I am a newbie)
function submitForm()
{
changeName();
printPDF();
sendEmail();
}
function changeName()
{
var title = document.getElementById('salesOrder').value
+" ";
title +=
document.getElementById('Project-Name').value
+" ";
title +=
document.getElementById('Client').value
+" ";
document.title = title;
}
function printPDF()
{
print();
}
function sendEmail()
{
var receivers = document.getElementsByClassName("receiver");
var emailString = "";
for (var i = 0; i < receivers.length; i++)
{
if (receivers[i].checked == true) {
emailString += receivers[i].getAttribute("email")
+";";
}
}
window.location.href = "mailto:"+emailString;
}

Related

return 2 values javascript

I'm trying to return two values in GAS like this:
Html
<script>
function phoneSearch() {
var phone = document.getElementById("phone").value;
google.script.run.withSuccessHandler(onSuccess).phoneSearch(phone);
}
function onSuccess(name, email) {
document.getElementById('nameDiv').innerHTML = "<div>" + name + "</div>";
document.getElementById('emailDiv').innerHTML = "<div>" + email+ "</div>";
}
</script>
Server JS
function phoneSearch(phone){
try {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("sheetNameHere");
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
if (data[i][4] == phone) {
var name = inputData[i][3];
var email = inputData[i][5];
return name;
return email;
}
}
} catch(e) {
alert(e)
}
}
Only the first value (name) returns. Thanks in advance!
You can't use more one 'return' at the same level, you should return object containing your two values.
return {
phone: phone,
email: email
}
You can only return 1 value from your function at a time, but you can easily wrap that in an object:
<script>
function phoneSearch() {
var phone = document.getElementById("phone").value;
google.script.run.withSuccessHandler(onSuccess).phoneSearch(phone);
}
function onSuccess(result) {
// ^ phoneSearch now returns -one- object, containing `name` and `email`.
document.getElementById('nameDiv').innerHTML = "<div>" + result.name + "</div>";
document.getElementById('emailDiv').innerHTML = "<div>" + result.email+ "</div>";
}
</script>
JS
function phoneSearch(phone){
try {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("sheetNameHere");
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
if (data[i][4] == phone) {
var name = inputData[i][3];
var email = inputData[i][5];
// Return both values at once.
return { name, email };
// (This is shorthand for: `return { name: name, email: email };`)
}
}
} catch(e) {
alert(e)
}
}

How to open and makes query to sqlite database with JavaScript

I'm trying to do a client-side database visualizer with JS. I have never used JavaScript for doing this type of stuff, so I searched online and I found out this web page. This is the script that I have done.
<html>
<head>
<script>
function handleFiles(files)
{
var file = files[0];
var reader = new FileReader();
reader.readAsBinaryString(file);
openSqliteDb(reader);
}
function openSqliteDb(reader)
{
setTimeout(function () {
if(reader.readyState == reader.DONE)
{
var database = SQL.open(bin2Array(reader.result));
document.write("2");
executeQuery("SELECT * FROM User", database);
}
else
{
openSqliteDb(reader);
}
}, 500);
}
function executeQuery(commands, database)
{
document.write("1");
commands = commands.replace(/n/g, '; ');
try {
var data = database.exec(commands);
document.write(data);
processData(data);
} catch(e) {
console.log(e);
}
}
function processData(data)
{
document.write("1");
var colHeaders = [];
var colMap = {};
var tableContent = [];
var tableHtml = [];
tableHtml.push("<table border='1' cellspacing='2' cellpadding-'3'>");
for (var i = 0; i < data.length; i++) {
tableContent.push("<tr>");
var dataElem = data[i];
if (dataElem instanceof Array) {
for (var j = 0; j < dataElem.length; j++) {
var element = dataElem[j];
if (element.column && !colMap[element.column]) {
colHeaders.push("<th>" + element.column + "</th>");
colMap[element.column] = colHeaders.length;
tableContent.push("<td> " + element.value + "</td>");
} else {
tableContent.push("<td> " + element.value + "</td>");
}
}
} else {
if (element.column && !colMap[element.column]) {
colHeaders.push(element.column);
colMap[element.column] = colHeaders.length;
}
}
tableContent.push("</tr>");
}
tableHtml.push(colHeaders.join(" "));
tableHtml.push(tableContent.join(" "));
tableHtml.push("</table>");
document.getElementById("table").innerHTML = tableHtml.join(" ");
}
</script>
</head>
<body>
<input type="file" id="input" onchange="handleFiles(this.files)">
</body>
</html>
I can't figure it out why it is not working, but I think that the problem is the line var database = SQL.open(bin2Array(reader.result)); in the openSqliteDb() function. Can somebody help me?

Javascript - Playing sound on variable change

I'll try to explain this as much as possible, but basically what I'm trying to do is create an Instant Messaging Service for my website to be used by privately by a group of people. I've got the sending and receiving down pat, but I can't seem to find a work around for the notification sounds.
What I would like is for a notification sound to play when a new message is received. I've been fiddling with it for about 45 minutes, and I've tried looking around for a solution, but I can't seem to find any avail.
Here is my Javascript:
function update() {
var xmlhttp=new XMLHttpRequest();
var username = "<?php echo $ugt ?>";
var output = "";
var number = 0;
var messages = msgarea.childElementCount / 2;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText.split("\n")
var rl = response.length
var item = "";
for (var i = 0; i < rl; i++) {
item = response[i].split("\\")
if (item[1] != undefined) {
if (item[0] == username) {
output += "<div class=\"msgsentby ext\"><div class='imgprop'></div></div><div class=\"msgc\"> <div class=\"msg msgfrom\">" + item[1] + "</div> <div class=\"msgarr msgarrfrom\"></div> </div>";
} else {
output += "<div class=\"msgsentby\"><img src='https://api.lspd-fibo.com/avatar/?gt=" + item[0] + "'></div> <div class=\"msgc\"> <div class=\"msg\">" + item[1] + "</div> <div class=\"msgarr\"></div></div>";
number = msgarea.childElementCount / 2;
}
}
}
msgarea.innerHTML = output;
if (messages < number ) {
audio.play();
setTimeout(function() {
messages = msgarea.childElementCount / 2;
}, 500);
}
msgarea.innerHTML += "number: " + number;
msgarea.innerHTML += " messages: " + messages;
}
}
xmlhttp.open("GET","get-messages.php?username=" + username,true);
xmlhttp.send();
}
function sendmsg() {
var message = msginput.value;
var delay = 1500;
if (message != "") {
var username = "<?php echo $ugt ?>";
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {}
}
message = escapehtml(message)
msgarea.innerHTML += "<div class=\"msgc\" style=\"margin-bottom: 30px;\"> <div class=\"msg msgfrom sending\">" + message + "</div> <div class=\"msgarr msgarrfrom\"></div> <div class=\"msgsentby msgsentbyfrom\">Sending...</div> </div>";
msginput.value = "";
var objDiv = document.getElementById("msg-area");
objDiv.scrollTop = objDiv.scrollHeight;
xmlhttp.open("GET","update-messages.php?username=" + username + "&message=" + message,true);
xmlhttp.send();
setTimeout(function() {
var objDiv = document.getElementById("msg-area");
objDiv.scrollTop = objDiv.scrollHeight;
}, delay);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var objDiv = document.getElementById("msg-area");
objDiv.scrollTop = objDiv.scrollHeight;
}
}
}
}
I apologize if it's a little messy, but this is what I have to work with. I appreciate any assistance that anyone can offer in advance :)
//EDIT 1
Personally, what I was thinking may work, was to count all of the messages when the page had loaded, and every time the script refreshed and a new number was detected, make a sound.. I've tried getting this particular method to work, also with no avail but that is likely due to my lack of knowledge in the process.
//EDIT 2
I've edited my JavaScript with what I've been trying to work with in the meantime.
I have taken your code, moved a few variable declarations up and switched to using Array.prototype.forEach() so we don't have to handle incrementing the index manually... This works for me...
function update() {
var xmlhttp=new XMLHttpRequest();
var username = "Fred";
var output = "";
var number = 0;
var oldVar; //initialize here instead of after forEach
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = xmlhttp.responseText.split("\n")
var rl = response.length
var item; //just declare array here but don't need to set value
response.forEach(function(responseLine,index) {
item = responseLine.split("\\")
if (item[1] != undefined) {
number++;
if (item[0] == username) {
output += "<div class=\"msgsentby\"><div class='imgprop'></div></div><div class=\"msgc\"> <div class=\"msg msgfrom\">" + item[1] + "</div> <div class=\"msgarr msgarrfrom\"></div> </div>";
} else {
output += "<div class=\"msgsentby\"><img src='https://api.lspd-fibo.com/avatar/?gt=" + item[0] + "'></div> <div class=\"msgc\"> <div class=\"msg\">" + item[1] + "</div> <div class=\"msgarr\"></div></div>";
}
}
});
if (oldVar != number) {
var audio = new Audio('notification.mp3');
audio.play();
}
msgarea.innerHTML = output;
//removed var keyword here so we don't re-declare it each time
oldVar = number;
}
}
xmlhttp.open("GET","get-messages.php?username=" + username,true);
xmlhttp.send();
}

Trouble with JavaScript onSubmit from HTML Form

This is probably a very simple oversight on my behalf, but I cannot get the below to run.
I am triggering this function at the press of a button in a form element. If I run this outside of the getData() function it works correctly (with a manually coded URL), but trying to run the function does not work.
Could someone please take a look at this and advise where I may be going wrong?
function getData(form) {
var url = '/service/search/' + form.date.value;
var getStats = new XMLHttpRequest();
getStats.open('GET', url, true);
getStats.onload = function() {
if (getStats.status = 200) {
//Add JSON to "data"
var data = JSON.parse(getStats.responseText);
var list = '';
if (data.length > 0) {
document.getElementById('results').innerHTML += '<tr><th> </th><th>Start</th><th>End</th><th>Title</th><th> </th></tr>'
}
for (var i = 0; i < data.length; i++) {
list += '<tr data-state="'+data[i].state+'" class="'+data[i].type+'-item"><td class="crm-'+data[i].state+'"></td><td>'+data[i].start+'</td><td>'+data[i].end+'</td><td>'+data[i].title+'</td><td>View</td>';
}
//document.getElementById('results').innerHTML += list;
console.log(list);
} else {
console.error('HTTP ERROR: ' + getStats.status);
}
};
getStats.onerror = function() {
console.error('ERROR');
};
getStats.send();
}
UPDATE
As requested, this is how I am calling the function:
<form onsubmit="getData(this);">
<input type="text" placeholder="YYYY-MM-DD" id="date">
<input type="submit" value="Search">
</form>
In regards to when this works correctly, if I run the below outside of a function it works:
var url = '/service/search/2014-06-11';
var getStats = new XMLHttpRequest();
getStats.open('GET', url, true);
getStats.onload = function() {
if (getStats.status = 200) {
//Add JSON to "data"
var data = JSON.parse(getStats.responseText);
var list = '';
if (data.length > 0) {
document.getElementById('results').innerHTML += '<tr><th> </th><th>Start</th><th>End</th><th>Title</th><th> </th></tr>'
}
for (var i = 0; i < data.length; i++) {
list += '<tr data-state="'+data[i].state+'" class="'+data[i].type+'-item"><td class="crm-'+data[i].state+'"></td><td>'+data[i].start+'</td><td>'+data[i].end+'</td><td>'+data[i].title+'</td><td>View</td>';
}
//document.getElementById('results').innerHTML += list;
console.log(list);
} else {
console.error('HTTP ERROR: ' + getStats.status);
}
};
getStats.onerror = function() {
console.error('ERROR');
};
getStats.send();

Something is preventing my submit button from functioning

I posted earlier regarding a different topic and want to thank all who helped me out. I've cleaned up my code pretty well (I think) but now when I click 'submit' nothing happens. I suspect the issue is regarding sending data with ajax but there is a problem with the way I've written things to send both text and radio data. Before posting this, I did do a thorough search to see if the question may have been answered in another's post but didn't find it - probably because I'm not astute enough with javascript to relate it to my own code. Anyway, here is a sample http://jsfiddle.net/J2yWQ/78/ If someone could find my issue I would really appreciate it.
function emailWarning() {
var check = document.getElementById("check");
check.className = 'show';
}
function validateEmail(xem) {
var re = /\S+#\S+\.\S+/;
return re.test(xem);
}
function postData() {
email = 'email'+document.getElementById('email').value;
var tt = validateEmail(email);
if (tt == true) {
xmlhttp.open('POST', 'payment.php', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send(myProps.join("&"));
} else {
emailWarning();
}
}
var myProps = [];
function insert() {
try {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
myProps = [];
function addProp(id) {
var input = document.getElementById(id);
if(!input) {
input = document.forms[0][id];
}
var value = encodeURIComponent(input.value);
myProps.push(id + "=" + value);
}
addProp('child_name');
addProp('age');
addProp('hometown');
addProp('boy_girl');
addProp('first_name');
addProp('last_name');
addProp('email');
addProp('address1');
addProp('address2');
addProp('city');
addProp('state');
addProp('zip');
addProp('country');
var flagInvalid = false;
var tempArray = document.getElementsByClassName("required");
for (var i = 0; i < tempArray.length; i++) {
if (tempArray[i].value == "") {
flagInvalid = true;
break;
}
}
if (flagInvalid == false) {
postData();
} else {
var showValidationError = function(id) {
var el = document.getElementById(id);
if (el) {
el.className = 'show';
}
else {
alert('Missing element #' + id);
}
}
showValidationError('log');
for (var i = 1; i < 12; i++) {
showValidationError('log' + i);
}
}
} catch (e) {
alert('An error occured in inert: ' + e);
}
}

Categories

Resources