Javascript:包含随机数的url
一位朋友正在从他的网站链接我的页面。 由于我需要用户在访问我的页面时避免缓存,因此我希望网址具有以下形式:
http: //www.mypage.com/index.php?456646556
其中 456646556 是随机数。
由于我的朋友没有安装php,我如何使用Javascript建立随机数的链接?
我的朋友还要求我只给他网址,没有其他功能,因为他的页面已经加载了这些功能。能做到吗?
多谢
A friend is linking my page from his site.
As I need users to avoid caching when visiting my page, I want the url to have this form:
http://www.mypage.com/index.php?456646556
Where 456646556 is random number.
As my friend does not have installed php, how can I build the link with the random number using Javascript?
Also my friend asked me to give him just the url, with no further functions as his page is already loaded with them. Can it be done?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会添加一个参数,但如果需要,您可以将其省略:
var url = "http://www.mypage.com/index.php?rnd="+Math.random()
或
< code>var url = "http://www.mypage.com/index.php?rnd="+new Date().getTime()
链接:
注意,如果您有多个赋值 - 例如在循环中,您需要添加到 getTime,因为循环的迭代速度快于一毫秒:
UPDATE 将 URL 构造函数与 searchParams 结合使用
I would add a parameter but you can leave it out if needed:
var url = "http://www.mypage.com/index.php?rnd="+Math.random()
or
var url = "http://www.mypage.com/index.php?rnd="+new Date().getTime()
Link:
Note that if you have more than one assignment - for example in a loop, you need to add to the getTime since an iteration of the loop is faster than a millisecond:
UPDATE to use the URL constructor with searchParams
它生成一个从 0(下)到 100000000(上)的随机 X,你可以设置你想要的范围;)
it generates a random X from 0(lower) to 100000000 (upper), you can obv set the bounds you want ;)
使用 Math.random():
小心你可能 从
Math.random
获得相同的输出两次,毕竟它是随机的。Use Math.random():
Beware you might get the same output twice from
Math.random
, after all it's random.