当用户单击按钮时获取当前用户的url

Obtain current user url when user clicks a button

本文关键字:用户 url 单击 按钮 获取      更新时间:2023-09-26

是这样的:一个用户去我的网站上有标签的页面,发现标签内容上有一个错误,然后他看到按钮下面的"报告"(这都在标签之外)。当他点击此按钮时,用户所在的当前url将通过电子邮件发送。

所有这个计划的最初的基本问题是:¿我怎么得到用户的当前url页面,当用户点击按钮"报告"?

in Javascript try

window.location.href

尝试使用Location.href,这会给你当前的网页URL

您可以同时使用window.location.hrefdocument.URL来获取当前页面的URL。

两者的区别是- window.location.href是可设置的,document.URL是不可设置的

我不能很好地得到这个工作与JSfiddle,因为试图导航到一个新的页面在他们的框架。但这是一个自包含的html示例,您应该能够粘贴到您的页面,并有工作。它打开客户端的默认电子邮件程序,用当前URL预填充正文。

    <style>
        div#reportButton {
            font-family: Verdana, sans-serif;
            width: 200px;
            background-color: #dd7777;
            border: 1px solid black;
            text-align: center;
            padding: 10px;
        }
    </style>
    <div id='reportButton'>Report a problem</div>
    <script>
        var button = document.getElementById("reportButton");
        button.onclick = function(){
            var currentURL = window.location.href;
            window.location.href="mailto:webmaster@example.com?subject=Bug+report&body=Problem+with+URL+" + currentURL;
        }
    </script>