So here is the link of the fiddle. I already tried a lots of searches and tried combining codes from other sites but cant seem to figure out the error. https://jsfiddle.net/itsmrchadd/fqjhu03r/2/
Based on the subject i just need pop up outlook with the ID in it. Sorry im a bit beginner here. :)
The random generator is already working i was able to test that separately. The only problem is the sending of email. If you try to click the link nothing happens, it would just load for a couple of seconds then stops.
function generateEmailID(length, chars) { "use strict"; //Set default values var result = ''; var timestamp = +new Date().toString(36).slice(2); //Set optional values length = length || 7; chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //Generate the id based on the parameters with timestamp for (var i = length; i > 0; --i) { result += chars[Math.round(Math.random() * (chars.length - 1))]; } return timestamp + result; } function sendEmail(email, subject, body) { "use strict"; //Set optional values email = email || "random@random.random"; subject = subject + " [" + generateEmailID() + "]" || "Test [" + generateEmailID() + "]"; body = body || "Test"; //Send email with id generated in the subject window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body; }
<a href="#" onclick="sendEmail()">Send Email</a>
The Fiddle doesn't work because somehow the link is being created before the js is created (but here in stackoverflow's "Run code snippet" your code does work).
(In JSFiddle hit F12 to see the console log in the browser for the error.)
To fix it in the JSFiddle you can refer to the function before creating the element as follows:
html:
<script>var sendEmail();</script>
<a href="#" onclick="sendEmail()">Send Email</a>
with your js having a small change:
sendEmail = function(email, subject, body) {
//(...rest of code stays the same)
or when you create it in a html page, just put the js in the head
<html>
<head>
<script>
//(...js here)
</script>
</head>
<body>
<!-- html link here -->
<a href="#" onclick="sendEmail()">Send Email</a>
</body>
</html>
...thus, the code would be as follows: (I added a few other changes, such as for code that didn't seem necessary ie .slice(2)
and setting subject = ''
when it is undefined...)
function generateEmailID(length, chars) { //"use strict"; //Set default values var result = ''; var timestamp = new Date().toString(36); //.slice(2); //console.log(timestamp); //Set optional values length = length || 7; chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //Generate the id based on the parameters with timestamp for (var i = length; i > 0; --i) { result += chars[Math.round(Math.random() * (chars.length - 1))]; } //console.log(timestamp + result); return timestamp + result; } sendEmail = function(email, subject, body) { //"use strict"; if(!subject) subject = ''; //Set optional values email = email || "random@random.random"; subject = subject + " [" + generateEmailID() + "]" || "Test [" + generateEmailID() + "]"; body = body || "Test"; //Send email with id generated in the subject window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body; }
<a href="#" onclick="sendEmail()">Send Email</a>
thanks for all the help. i already got what i wanted..... its also now working fine together with my htmls
here is the link of the final fiddle.... https://jsfiddle.net/itsmrchadd/fqjhu03r/3/
function generateEmailID(length, chars) { "use strict"; //Set default values var result = ''; var timestamp = +new Date().toString(36).slice(2); //Set optional values length = length || 7; chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //Generate the id based on the parameters with timestamp for (var i = length; i > 0; --i) { result += chars[Math.round(Math.random() * (chars.length - 1))]; } return timestamp + result; } function sendEmail(email, subject, body) { "use strict"; //Set default values if (!subject) { subject = "Test"; } //Set optional values email = email || "random@random.random"; subject = subject + " [" + generateEmailID() + "]" || "Test"; body = body || "Test"; //Send email with id generated in the subject window.location.href = "mailto:" + email + "?subject=" + subject + "&body=" + body; }
<a href="#" onclick="sendEmail()">Send Email</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.