Javascript 如何在“mailto”正文链接中写入当前页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7977165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write in 'mailto' body link to current page
提问by Chuck Norris
I have mailto
button in all pages of my site and I want to write reference to that page in e-mail body.
mailto
我网站的所有页面都有按钮,我想在电子邮件正文中写入对该页面的引用。
In one page I can have
在一页中,我可以拥有
<a class="email" title="Email a friend" href="mailto:?subject=Interesting%20information&body=I thought you might find this information interesting:%20%0d%0ahttp://www.a.com/Home/SiteMap/tabid/589/Default.aspx">Email</a>
But how can I make this generic for all pages?
但是我怎样才能使所有页面通用呢?
回答by reticent
This is the pure javascript solution:
这是纯javascript解决方案:
<a class="email" title="Email a friend" href="#" onclick="javascript:window.location='mailto:?subject=Interesting information&body=I thought you might find this information interesting: ' + window.location;">Email</a>
回答by Shadow2531
With Javascript, you utf-8-percent-encode the subject and body hfvalues using encodeURIComponent() on a UTF-8 page.
使用 Javascript,您可以在 UTF-8 页面上使用 encodeURIComponent() 对主题和正文 hfvalues 进行 utf-8-percent 编码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script>
function SendLinkByMail(href) {
var subject= "Interesting Information";
var body = "I thought you might find this information interesting:\r\n\r\n<";
body += window.location.href;
body += ">";
var uri = "mailto:?subject=";
uri += encodeURIComponent(subject);
uri += "&body=";
uri += encodeURIComponent(body);
window.open(uri);
}
</script>
</head>
<body>
<p><a href="javascript:(function()%7BSendLinkByMail()%3B%7D)()%3B">Email link to this page</a></p>
</body>
</html>
If you're doing this server-side, you can just build the mailto link and emit it as the href attribute value. Then, you won't need JS at all.
如果您在服务器端执行此操作,则只需构建 mailto 链接并将其作为 href 属性值发出。然后,您将根本不需要 JS。
I assume ASP has some URI encoding functions that work like encodeURIComponent().
我假设 ASP 有一些像 encodeURIComponent() 那样工作的 URI 编码函数。
You can also view the source of my mailto URI composerpage as another example.
作为另一个示例,您还可以查看我的mailto URI composer页面的源代码。
You an also take a look at http://shadow2531.com/opera/testcases/mailto/mailto_uri_scheme_idea.html#send_link_by_mailand my mailto URI syntaxvalidator.
您还可以查看http://shadow2531.com/opera/testcases/mailto/mailto_uri_scheme_idea.html#send_link_by_mail和我的mailto URI语法验证器。
For the < and > that I encase the URI in, in the JS code above, see "Appendix C. Delimiting a URI in Context" of RFC3986 for why.
对于我在上面的 JS 代码中包含 URI 的 < 和 >,请参阅 RFC3986 的“附录 C. 在上下文中分隔 URI”了解原因。
Also, instead of window.location.href, you can use window.location or document.location.href or document.location. I normally use "document.location".
此外,您可以使用 window.location 或 document.location.href 或 document.location 来代替 window.location.href。我通常使用“document.location”。
For why you use a Javascript URI instead of an onclick attribute, see this answer.
有关为什么使用 Javascript URI 而不是 onclick 属性的原因,请参阅此答案。
Also note that in the JS URI in the code above, I wrapped the code in an anonymous function. That's not necessary in this case because the function doesn't return anything that would change the document when you click. But, it's to just do it all the time for good measure.
还要注意,在上面代码中的 JS URI 中,我将代码包装在一个匿名函数中。在这种情况下,这不是必需的,因为该函数不会返回任何会在您单击时更改文档的内容。但是,这只是为了更好的衡量而一直这样做。
See my Javascript URI composeto help with creating javascript URIs.
请参阅我的Javascript URI compose以帮助创建 javascript URI。
回答by Nicu Surdu
You can put a JS on every page that can modify all the hrefs of all email link when the document loads.
您可以在每个页面上放置一个 JS,它可以在文档加载时修改所有电子邮件链接的所有 href。
I'll use jQuery for the example code because is more compact, but this can be achieved also with pure JS:
我将在示例代码中使用 jQuery,因为它更紧凑,但这也可以使用纯 JS 来实现:
$(document).ready(function(){
$(".email").attr("href", "mailto:?subject=Interesting%20information&body=I thought you might find this information interesting:%20%0d%0a"+window.location);
});
回答by david
you need to use &body=
to include a string in the email body
您需要使用&body=
在电子邮件正文中包含一个字符串
here is a generic javascript function
这是一个通用的 javascript 函数
<script>
function emailFriend(){
var strrep ,ptitle = document.title;
strrep= ptitle.replace(/"/g,'%22');
strrep= ptitle.replace(/&/g,'%26');
var mailtourl = "mailto:?subject=Interesting%20information&body=I thought you might find this information interesting: "+encodeURIComponent(location.href);
location.href = mailtourl;
return false
}
</script>
<a href="javascript:;" onclick="emailFriend();return false">Email</a>