Javascript:包含随机数的url

发布于 2024-10-11 18:17:04 字数 321 浏览 1 评论 0原文

一位朋友正在从他的网站链接我的页面。 由于我需要用户在访问我的页面时避免缓存,因此我希望网址具有以下形式:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绿光 2024-10-18 18:17:04

我会添加一个参数,但如果需要,您可以将其省略:

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

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

UPDATE 将 URL 构造函数与 searchParams 结合使用

const addRnd = urls => {
  let rnd = new Date().getTime();
  return urls.map((urlStr,i) => {
    let url = new URL(urlStr);
    url.searchParams.set("rnd",rnd+i);  // in case called multiple times
    return url;
  });
};
const urls = addRnd( ["http://www.mypage.com/index1.php","http://www.mypage.com/index2.php","http://www.mypage.com/index3.php"])
console.log(urls)

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:

<a href="http://www.mypage.com/index.php?rnd=1" onClick="this.href=this.href.split('?')[0]+'?rnd='+new Date().getTime()">Mostly random</a>

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:

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

UPDATE to use the URL constructor with searchParams

const addRnd = urls => {
  let rnd = new Date().getTime();
  return urls.map((urlStr,i) => {
    let url = new URL(urlStr);
    url.searchParams.set("rnd",rnd+i);  // in case called multiple times
    return url;
  });
};
const urls = addRnd( ["http://www.mypage.com/index1.php","http://www.mypage.com/index2.php","http://www.mypage.com/index3.php"])
console.log(urls)

羅雙樹 2024-10-18 18:17:04
<a href="http://www.mypage.com/index.php?" onclick="this.href+=new Date().getTime();return true;">link</a>
<a href="http://www.mypage.com/index.php?" onclick="this.href+=new Date().getTime();return true;">link</a>
枕头说它不想醒 2024-10-18 18:17:04
var lower = 0;
var upper = 100000000;
var url = "http://www.mypage.com/index.php?"+(Math.floor(Math.random()*(upper-lower))+lower)

它生成一个从 0(下)到 100000000(上)的随机 X,你可以设置你想要的范围;)

var lower = 0;
var upper = 100000000;
var url = "http://www.mypage.com/index.php?"+(Math.floor(Math.random()*(upper-lower))+lower)

it generates a random X from 0(lower) to 100000000 (upper), you can obv set the bounds you want ;)

脸赞 2024-10-18 18:17:04

使用 Math.random()

 // navigate to the new page and append a random number at the end of the url
 window.location.href = newUrl + '?' Math.random();

小心你可能Math.random 获得相同的输出两次,毕竟它是随机的。

Use Math.random():

 // navigate to the new page and append a random number at the end of the url
 window.location.href = newUrl + '?' Math.random();

Beware you might get the same output twice from Math.random, after all it's random.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文