【问题标题】:Capture url, browser and browser version, and put it in body of an email捕获 url、浏览器和浏览器版本,并将其放在电子邮件正文中
【发布时间】:2014-01-16 13:40:57
【问题描述】:

我有一个包含数千页的网站,其中包含随着技术和设备的进步而可能出现的各种问题 (www.toilette-humor.com)。我需要在每个页面上添加一个链接,以允许用户“报告问题”。所以我需要获取以下信息并将其通过电子邮件发送给我:

  • 他们在哪个 URL 上看到了问题
  • 他们使用的是什么浏览器和版本

如果可能的话……

  • 他们使用的是什么操作系统
  • 他们正在使用什么平台/设备

我在这里找到了一些代码来回答捕获 URL 并将其放入电子邮件正文的第一个问题,并且我找到了一些用于捕获浏览器和版本的其他代码,但该代码并未将其添加到一封电邮。

我对编写 JavaScript 一无所知。所以请问大家,这两个脚本如何结合,或者有没有更好的解决方案。

脚本一(捕获 URL 并将其添加到电子邮件正文):How to write in 'mailto' body link to current page

<head>
<script>
    function SendLinkByMail(href) {
        var subject= "Report a Problem";
        var body = "There is a problem with this webpage:\r\n\r\n<";
        body += window.location.href;
        body += ">";
        var uri = "mailto:?subject=";
        uri += encodeURIComponent(subject);
        uri += "&body=";
        uri += encodeURIComponent(body);
        window.location.href = uri;
    }
</script>
</head>

<body>
    <p><a href="javascript:(function()%7BSendLinkByMail()%3B%7D)()%3B">Email link to this page</a></p>
</body>

在此处找到脚本二(捕获浏览器和版本):How can you detect the version of a browser?

<script>
    navigator.sayswho= (function(){
        var ua= navigator.userAgent, tem, 
        M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
        if(/trident/i.test(M[1])){
            tem=  /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
            return 'IE '+(tem[1] || '');
        }
        M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
        if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
        return M.join(' ');
    })();
</script>

提前感谢大家的时间和知识。

【问题讨论】:

  • 您在哪里找到了 HTML 位?看起来像一些您不想要的额外代码 %7B 等。
  • 在此页面上查看一些 cmets:stackoverflow.com/questions/7977165/… 该代码对我来说确实可以正常工作。只需要弄清楚如何将更多信息添加到正文(浏览器和版本)

标签: javascript email


【解决方案1】:

作为仅供参考,您不应该使用 javascript 链接,但我们会在稍后处理。

如果你想将 userAgent 添加到 body,你可以这样做:

function SendLinkByMail(href) {
    var subject= "Report a Problem";
    var body = "There is a problem with this webpage:\r\n\r\n<";
    body += window.location.href;
    body += ">";
    body += navigator.userAgent;
    var uri = "mailto:?subject=";
    uri += encodeURIComponent(subject);
    uri += "&body=";
    uri += encodeURIComponent(body);
    window.location.href = uri;
}

如果你想使用你已经拥有的那个奇怪的功能,你可以将navigator.userAgent 行更改为navigator.sayswho()

此外,您应该使用事件侦听器附加操作:

<body>
    <p class="send-email">Email link to this page</p>
    <script>
        var send = document.getElementsByClassName('send-email')[0];
        send.attachEvent? send.attachEvent('click', SendLinkByMail) : send.addEventListener('click', SendLinkByMail);
    </script>
</body>

但这仍然是一种泄漏的方式来发送电子邮件,因为如果用户使用网络邮件客户端mailto:,如果没有用户的额外设置工作,链接不太可能工作。

【讨论】:

  • 感谢您的回复。我认为 mailto: 可能是出于这个确切原因的问题。到目前为止,上面的脚本是我能找到的所有内容。此时我的 Google Foo 无法找到一段好的代码来解决我的问题。我想我可能需要将我的方向更改为可以填充我需要的信息的表单,然后以这种方式提交而不是通过用户电子邮件程序,但这将是我想的另一个主题。我会试试你上面的脚本。再次感谢您
  • 是的,这正是我的标题所寻找的。除了您提出的 mailto: 问题之外,我将研究一种更好的方法来执行此操作。亲切的问候
  • @BGyver 执行此操作的“正确”方法是从您的后端发送邮件,但这是一个巨大的蠕虫罐头。这种方式足以满足一定数量的用例
  • @BGyver 如果这可行,您应该支持/接受答案,以便其他人知道这是解决问题的有效解决方案。
猜你喜欢
  • 2011-01-22
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
相关资源
最近更新 更多