Chu*_*ris 10 html javascript href
我的mailto网站的所有页面都有按钮,我想在电子邮件正文中写入该页面的引用.
在一个页面我可以
<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>
Run Code Online (Sandbox Code Playgroud)
但是,我怎样才能为所有页面制作这个通用名称?
ret*_*ent 34
这是纯粹的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>
Run Code Online (Sandbox Code Playgroud)
使用Javascript,您可以在UTF-8页面上使用encodeURIComponent()对主题和正文hfvalue进行utf-8%编码.
<!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>
Run Code Online (Sandbox Code Playgroud)
如果您正在执行此服务器端,则可以构建mailto链接并将其作为href属性值发出.然后,你根本不需要JS.
我假设ASP有一些URI编码函数,就像encodeURIComponent()一样.
您还可以查看mailto URI composer页面的源代码作为另一个示例.
您还可以查看http://shadow2531.com/opera/testcases/mailto/mailto_uri_scheme_idea.html#send_link_by_mail和我的mailto URI 语法验证程序.
对于我包含URI的<和>,在上面的JS代码中,请参阅RFC3986中的"附录C.在上下文中划分URI"的原因.
另外,您可以使用window.location或document.location.href或document.location代替window.location.href.我通常使用"document.location".
为什么使用Javascript URI而不是onclick属性,请参阅此答案.
另请注意,在上面代码中的JS URI中,我将代码包装在匿名函数中.在这种情况下,这不是必需的,因为当您单击时,该函数不会返回任何会更改文档的内容.但是,它只是为了衡量标准而一直这样做.
请参阅我的Javascript URI撰写以帮助创建JavaScript URI.