javascript - JS-variable used with mailto is not updated -
encountered problem - want include js-variable in body of mail, users can forward friends, using following:
<head> <script src="javascript.js" type="text/javascript"> </script> <body> <script type="text/javascript"> document.write('<a href="mailto:?subject=subject&body=bodytext' +total+ '">mail me</a>');</script> </body>
the var total defined in .js file, , it's value can manipulated executing af function attached form.
even after function carried out , page displays changes, opened mail display default value of var total instead of manipulated.
basically question is: how modified var total display inside mailto-link, instead of default var total.
disclaimer: i'm not smart (when comes coding @ least).
bonus-info: i've made possible share var total facebook using fb.init etc., displays manipulated var.
edit:
js (cut bit short):
var total = 'empty'; function getcalculation() { total = (parseint(f1) + parseint(f2)).tofixed(0); document.contactform.formtotal.value = total; }
the document.write run once when page generated. not dynamic code gets triggered every time changes. need different approach this.
instead of document.write, make normal html link (a-element) javascript href change document url mailto link.
for example:
<a href="javascript:location.href='mailto:my@email.com?subject=subject&body=bodytext' + total">mail me</a>
this means when link clicked, instead of going url, execute piece of javascript changes document's location mailto-link, , embeds value of total -variable body.
even better way create external function trigger when link clicked:
<script type="text/javascript"> function sendmail() { var email = 'my@email.com'; var subject = 'my subject'; var body = 'this total: ' + total; location.href = 'mailto:' + email + '?subject=' + encodeuricomponent(subject) + '&body=' + encodeuricomponent(body); } </script>
and in link add following href:
<a href="javascript:sendmail()">mail me</a>
now total
variable gets embedded in email body every time link clicked, instead of when page loaded.
the encodeuricomponent makes sure subject , body encoded properly, special characters , spaces, etc gets translated correctly in mail client.
i hope helps!
Comments
Post a Comment