1

I have a URL that contains parameters. I'm trying to create a mailto command that will open the link of the current window's URL. I am currently using the following JS to call the function, but it truncates the URL after the "&" sign.

<a href="mailto:?subject=Report&body=[sub]" onclick="this.href = this.href.replace(''[sub]'',window.location)">email</a>

This returns "http://www.mydomain.com/reportname?SiteCode=0027" in the body of the email, but the URL is actually listed below as it is passing variables: http://www.mydomain.com/reportname?SiteCode=0027&CustomerCode=ALLCUSTOMERS&JobCode=ALLJOBS

How can I output the entire URL including the parameters to an email?

4

1 回答 1

1

我做了一个小的jsFiddle。我做了两个小改动。

  1. 首先,我更改了 href 属性 onClick,并将其设置在DOMContentLoaded上。
  2. 其次我不得不使用 encodeURIComponent 因为任何特殊的字符都会破坏你的脚本

归功于这个问题博士。谷歌


<html>
<head>
    <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function() {
            var simpleHref = document.getElementById("simpleHref");
            simpleHref.href = "mailto:asdf@asdf.com?subject=Report&body=" + encodeURIComponent(window.location);
        });
    </script>
</head>
<body>
    <a href="" id="simpleHref">Test</a>
</body>
</html>
于 2013-09-16T21:52:14.303 回答