javascript - 重定向联系表 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
解决方案
您可以通过在目标页面上添加以下代码段轻松地做到这一点:
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
推荐阅读
- react-native - How do I get my React Native component to respond to whether a timer is running?
- python - Python argparse:在命令中传递参数时无法识别的参数
- algorithm - 带权重的 Karger 算法
- python - 生产者-多消费者问题,消费者在生产者完成之前完成工作
- python - numpy中基于内核提取子数组
- angular - 获取父路由参数的更简单方法 - 角度路由器
- apache-spark - Py4JJava错误:。:org.apache.spark.SparkException:无效的 Spark URL:spark://HeartbeatReceiver@kshitij_computer.mshome.net:50675
- terminal - 来自单个文本文件的多个绘图 (gnuplot)
- django - Django Rest Framework 中嵌套 URL 的相关资源
- css - 保持 100% 的网页浏览高度