[英]Bookmarklet to add a parameter at end of each URL
我正在嘗試創建一個簡單的書簽,它在任何 URL 的末尾添加一個短參數
我使用 ?clearcache 清空內部緩存,所以為了不每次都輸入它,我正在尋找一個簡單的解決方案
我的網站是
http://subdomain.mysite.net/
和書簽我想讓網址去
http://subdomain.mysite.net/?clearcache
與更深的頁面相同,因為我希望它去
http://subdomain.mysite.net/some-page/?clearcache
我目前正在嘗試
javascript:location=location.href.replace(/http:/g,"/?clearcache")
但它不起作用
當我點擊那個書簽時,我的網址變成
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
或使用?
.
對於那些想知道如何為“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");})());
這里建立在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); })());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.