我正在尝试创建一个简单的书签,它可以在任何 URL 末尾添加一个短参数
我使用 ?clearcache 清空内部缓存,因此为了避免每次都键入它,我正在寻找一个简单的解决方案
我的网站是
http://subdomain.mysite.net/
并且通过书签我想让 URL 转到
http://subdomain.mysite.net/?clearcache
与更深的页面相同,因为我希望它转到
http://subdomain.mysite.net/some-page/?clearcache
我目前正在尝试
javascript:location=location.href.replace(/http:/g,"/?clearcache")
但它不起作用
当我单击该书签时,我的 URL 变为
http://subdomain.mysite.net/?clearcache//subdomain.mysite.net/
我觉得我已经很接近了,但我只需要专家的一点提示。 我希望得到答复。 谢谢
这应该可以解决您的问题:
代码
javascript:void((function(){var loc = location.href; loc.indexOf("?") == -1 ? (location.href = loc+"?clearcache") : (location.href = loc+"&clearcache");})());
解释
检查是否存在任何其他查询字符串参数。如果是,则使用
clearcache
在末尾附加 &
或使用 ?
附加到 URL。
基于 Vikram Deshmukh 的答案,这是一个使用不同的缓存清除参数并在每次使用时替换它的版本。
扩展版:
javascript:void((function () {
'use strict';
let href;
if (document.location.search === '') {
href = document.location.href + '?_=' + Date.now();
} else {
let params = new URLSearchParams(document.location.search.substring(1));
if (params.get('_') === null) {
href = document.location.href + '&_=' + Date.now();
} else {
params.set('_', Date.now());
href= document.location.href.substring(0, document.location.href.indexOf('?') + 1) + params.toString();
}
}
document.location.assign(href);
})());
紧凑型:
javascript:void((function () { let href; if (document.location.search === '') { href = document.location.href + '?_=' + Date.now(); } else { let params = new URLSearchParams(document.location.search.substring(1)); if (params.get('_') === null) { href = document.location.href + '&_=' + Date.now(); } else { params.set('_', Date.now()); href= document.location.href.substring(0, document.location.href.indexOf('?') + 1) + params.toString(); } } document.location.assign(href); })());
对于那些想知道如何为“mod_pagespeed”执行此操作的人来说,这是一个有用的答案,完全受到上述答案的启发。
javascript:void((function(){var loc = location.href; if (loc.indexOf('PageSpeed') >= 0) return; loc.indexOf("?") < 0 ? (location.href = loc+"?PageSpeed=off") : (location.href = loc+"&PageSpeed=off");})());
较短版本
javascript:((param = 'yourParam=yourValue') => {const loc = location.href; if (loc.contains(param)) return; location.href = loc + (loc.contains('?') ? '&' : '?') + param})()