Javascript:包含随机数的 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4667927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:33:20  来源:igfitidea点击:

Javascript: url containing random number

javascripturlhyperlinkrandomnumbers

提问by ADM

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:

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

http://www.mypage.com/index.php?456646556

http://www.mypage.com/index.php?456646556

Where 456646556 is random number.

其中 456646556 是随机数。

As my friend does not have installed php, how can I build the link with the random number using Javascript?

由于我的朋友没有安装 php,我如何使用 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

非常感谢

回答by mplungjan

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="+Math.random()

or

或者

var url = "http://www.mypage.com/index.php?rnd="+new Date().getTime()

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>

Notethat 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:

请注意,如果您有多个分配 - 例如在循环中,您需要添加到 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);
}

UPDATEto use the URL constructor with searchParams

更新以使用带有 searchParams 的 URL 构造函数

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)

回答by stecb

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

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

回答by igor

<a href="http://www.mypage.com/index.php?" onclick="this.href+=new Date().getTime();return true;">link</a>

回答by Ivo Wetzel

Use Math.random():

使用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 mightget the same output twice from Math.random, after all it's random.

请注意,您可能会从 获得两次相同的输出Math.random,毕竟它是随机的。