0

我有一个网址。让我们假设它总是看起来像这样(来自服务器)https://testurl.com/w/img=67dj455j8j:。

如您所见,有w参数。未定义,就像上面一样,它返回具有标准宽度值的图像。会w_700: https://testurl.com/w_700/img=67dj455j8j返回宽度为700px虽然的图像。

如何在客户端以编程方式动态更改该参数?就屏幕尺寸而言,最佳宽度(UX 和带宽)是多少?1:1?

4

2 回答 2

2

也许是这样:

const width = 800;
const re = /\/w(_\d+)?/;
const url1 = new URL("https://testurl.com/w/img=67dj455j8j");
const url2 = new URL("https://testurl.com/w_700/img=67dj455j8j");

url1.pathname = url1.pathname.replace(re,"\w"+width);
console.log(url1);
url2.pathname = url2.pathname.replace(re,"\w"+width);
console.log(url2);

于 2020-06-24T09:24:31.057 回答
1

尝试这个:

window.location.href = window.location.href.replace('/w/', '/w_700/')
于 2020-06-24T09:22:29.440 回答