Javascript:包含随机数的网址 [英] Javascript: url containing random number

查看:119
本文介绍了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

其中456646556是随机数。

Where 456646556 is random number.

由于我的朋友没有安装php,如何使用Javascript构建带有随机数的链接?

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?

非常感谢

推荐答案

我会添加参数,但如果需要,可以将其保留:

I would add a parameter but you can leave it out if needed:

var url =http://www.mypage.com/index.php ?rnd =+ Math.random()

var url =http://www.mypage.com/index.php?rnd=\"+new日期()。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,因为循环的迭代速度比毫秒快:

Note that if you have more than one assignment - for example in a loop, you need to add to the getTime since and iteration of the loop is faster than a millisecond:

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);
}

这篇关于Javascript:包含随机数的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆