【问题标题】:Replacing space in url with code用代码替换url中的空格
【发布时间】:2013-10-04 12:19:21
【问题描述】:

我有这个代码。

我为在我的网站上推广该页面而设置的。

<script ... > 
document.write('<a href="mailto:?subject=my%20subject&body='+top.location.href+'">mail this link to a friend</a>'); 
</script>

但是在我的网址中,它在我的大多数广告系列中都有一个空间。这打破了我的促销电子邮件中的 url,因此直到第一个空格的 http:// 是超链接的。

如何将我的 url 中的空格替换为上面代码中的空格代码,这意味着整个链接将在电子邮件中超链接?

【问题讨论】:

  • 您想替换top.location.href 中的空格吗?就这么简单:top.location.href.replace(/\s/g, '%20')

标签: javascript php email


【解决方案1】:
document.write('<a href="mailto:?subject=my%20subject&body=' + encodeURIComponent(top.location.href) + '">mail this link to a friend</a>');

【讨论】:

    【解决方案2】:

    您必须先对 url 进行编码,例如。使用encodeURIComponent 函数。

    encodeURIComponent(top.location.href);
    

    【讨论】:

      【解决方案3】:
      <script ... > 
      var url2 = encodeURIComponent(top.location.href);
      
      document.write('<a href="mailto:?subject=my%20subject&body='+url2 +'">mail this link to a      friend</a>'); 
      

      【讨论】:

        猜你喜欢
        • 2014-05-20
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 2011-03-27
        • 1970-01-01
        • 2014-08-19
        相关资源
        最近更新 更多