【问题标题】:How to write in 'mailto' body link to current page如何在“mailto”正文中写入当前页面的链接
【发布时间】:2023-03-31 22:39:02
【问题描述】:

我网站的所有页面中都有mailto 按钮,我想在电子邮件正文中写下对该页面的引用。

在一页中我可以拥有

<a class="email" title="Email a friend" href="mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0ahttp://www.a.com/Home/SiteMap/tabid/589/Default.aspx">Email</a>

但是我怎样才能使所有页面都通用呢?

【问题讨论】:

  • Javascript 可以为您做到这一点。通过使用“document.location.href”。或者,如果您使用的是 .php,您可以更轻松地进行操作。可以用php吗?告诉我,我可以为你做一个例子!
  • 不,我使用 Asp ;) 但是我如何在这部分编写 javascript?(body="...")

标签: javascript html href


【解决方案1】:

使用 Javascript,您可以在 UTF-8 页面上使用 encodeURIComponent() 对主题和正文 hfvalue 进行 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>

如果您在服务器端执行此操作,您可以构建 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 syntax validator

对于我在上面的 JS 代码中包含 URI 的 ,请参阅 RFC3986 的“Appendix C. Delimiting a URI in Context”了解原因。

此外,您可以使用 window.location 或 document.location.href 或 document.location 来代替 window.location.href。我通常使用“document.location”。

关于为什么使用 Javascript URI 而不是 onclick 属性,请参阅this answer

还要注意,在上面代码中的 JS URI 中,我将代码包装在了一个匿名函数中。在这种情况下,这不是必需的,因为该函数不会返回任何会在您单击时更改文档的内容。但是,这只是为了更好的衡量标准。

请参阅我的 Javascript URI compose 以帮助创建 javascript URI。

【讨论】:

  • 不错的邮件验证 Shadow2531
  • 有没有办法阻止它打开一个新的空白标签/窗口?
  • @Jason 您可以将 window.open(uri) 更改为 window.location.href = uri
【解决方案2】:

您需要使用&amp;body= 在电子邮件正文中包含一个字符串

这是一个通用的 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>

【讨论】:

    【解决方案3】:

    这是纯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>
    

    【讨论】:

    • 可能会导致页面发生变化。返回 false 以防止这种情况发生。 ... + window.location;return false;"> ...
    • @TravisSwientek 你能解释一下为什么会这样吗?
    • 这会在当前窗口中打开电子邮件客户端。有什么办法可以让它在新标签页中打开?谢谢。
    • 添加目标="_blank"
    【解决方案4】:

    您可以在每个页面上放置一个JS,以便在文档加载时修改所有电子邮件链接的所有href。

    我将使用 jQuery 作为示例代码,因为它更紧凑,但也可以使用纯 JS 来实现:

    $(document).ready(function(){
        $(".email").attr("href", "mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0a"+window.location);
    });
    

    【讨论】:

    • 当然可以用纯javascript实现:),什么是jquery?
    • @david:“jQuery 是一个快速而简洁的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互以实现快速 Web 开发。jQuery 旨在改变您编写 JavaScript 的方式。”
    • 谢谢 nick,我只是在挖你一把 :} what is jqueryjavascript
    • 我已经使用了你的代码,第一个正文没有传递给电子邮件 b'coz "&"当我更改为“&”时,它就起作用了。最后,您的想法很适合通过 JQuery 传递当前链接 :) 谢谢。
    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2017-09-13
    • 2014-12-27
    • 1970-01-01
    • 2018-04-21
    • 2017-07-03
    相关资源
    最近更新 更多