首页 > 解决方案 > 重定向联系表 7 后生成唯一 url

问题描述

我用这个代码

<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
       location = 'https://www.example.com/thank-you/';
}, false );
</script>

此代码重定向我https://www.example.com/thank-you/

我希望这个重定向我https://www.example.com/thank-you/fdsffdfggfh(每次都是唯一链接),但向我展示 https://www.example.com/thank-you/ 这个页面内容。

我试试这个和这个工作

document.addEventListener( 'wpcf7mailsent', function( event ) {
            var length = 10;
            var res = '';
           var chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
           var charLen = chars.length;
           for ( var i = 0; i < length; i++ ) {
              res += chars.charAt(Math.floor(Math.random() * charLen));
           }
        location = 'http://example.com/thank-you/?'+res;
}, false );

谢谢@pagalprogrammer

标签: javascriptphpwordpress

解决方案


您可以通过在目标页面上添加以下代码段轻松地做到这一点:

function random_string(length) {
   var res = '';
   var chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
   var charLen = chars.length;
   for ( var i = 0; i < length; i++ ) {
      res += chars.charAt(Math.floor(Math.random() * charLen));
   }
   return "/"+res;
}
window.history.pushState("", "", window.location.href+random_string(10));

你也可以放一些其他的随机字符串函数。此代码使用 History API


推荐阅读