朋友正在从他的网站链接我的网页。 由于我需要用户在访问我的页面时避免缓存,我希望网址具有以下格式:
http://www.mypage.com/index.php?456646556
其中456646556是随机数。
由于我的朋友没有安装php,如何使用Javascript构建带有随机数的链接?
我的朋友也让我给他一个网址,没有其他功能,因为他的页面已经加载了它们。可以吗?
非常感谢
答案 0 :(得分:15)
我会添加一个参数,但如果需要可以将其保留:
var url = "http://www.mypage.com/index.php?rnd="+Math.random()
或
var url = "http://www.mypage.com/index.php?rnd="+new Date().getTime()
链接:
<a href="http://www.mypage.com/index.php?rnd=1" onClick="this.href=this.href.split('?')[0]+'?rnd='+new Date().getTime()">Mostly random</a>
注意如果您有多个赋值 - 例如在循环中,则需要添加到getTime,因为循环的迭代速度快于毫秒:
var rnd = new Date().getTime();
for (var i=0;i<links.length;i++) {
links[i].href = "http://www.mypage.com/index.php?rnd="+(rnd+i);
}
答案 1 :(得分:2)
var lower = 0;
var upper = 100000000;
var url = "http://www.mypage.com/index.php?"+(Math.floor(Math.random()*(upper-lower))+lower)
它生成一个从0(下)到100000000(上)的随机X,你可以obv设置你想要的界限;)
答案 2 :(得分:2)
<a href="http://www.mypage.com/index.php?" onclick="this.href+=new Date().getTime();return true;">link</a>
答案 3 :(得分:0)
// navigate to the new page and append a random number at the end of the url
window.location.href = newUrl + '?' Math.random();
请注意,可能从Math.random
获取两次相同的输出,毕竟它是随机的。