I'm trying to have a simple bookmarklet which adds a short parameter at end of any URL
I use ?clearcache to empty an internal cache, so in order not to type it each time, I'm looking for a simple solution
My site is
/
and with the bookmarklet I want to make the URL go to
/?clearcache
Same with deeper pages, as I would like it to go to
/?clearcache
I'm currently trying with
javascript:location=location.href.replace(/http:/g,"/?clearcache")
but it isn't working
When I click on that bookmarklet, my URL bees
/?clearcache//subdomain.mysite/
I feel I am close to it, but I just need a small tip from experts. I hope for a a reply. Thanks
I'm trying to have a simple bookmarklet which adds a short parameter at end of any URL
I use ?clearcache to empty an internal cache, so in order not to type it each time, I'm looking for a simple solution
My site is
http://subdomain.mysite/
and with the bookmarklet I want to make the URL go to
http://subdomain.mysite/?clearcache
Same with deeper pages, as I would like it to go to
http://subdomain.mysite/some-page/?clearcache
I'm currently trying with
javascript:location=location.href.replace(/http:/g,"/?clearcache")
but it isn't working
When I click on that bookmarklet, my URL bees
http://subdomain.mysite/?clearcache//subdomain.mysite/
I feel I am close to it, but I just need a small tip from experts. I hope for a a reply. Thanks
Share Improve this question edited Dec 19, 2013 at 23:40 user2988142 4273 silver badges12 bronze badges asked Dec 19, 2013 at 9:37 valdronivaldroni 1681 gold badge3 silver badges19 bronze badges 1-
In case someone would like to REPLACE parameters and ended up on this question, the solution would be simple:
javascript:void((function(){window.location.search="?clearcache";})());
– Kyborek Commented Oct 15, 2018 at 6:06
4 Answers
Reset to default 7This should solve your problem:
Code
javascript:void((function(){var loc = location.href; loc.indexOf("?") == -1 ? (location.href = loc+"?clearcache") : (location.href = loc+"&clearcache");})());
Explanation
Check if any other query string parameters exists. If yes, then appends clearcache
at the end using an &
or append to the URL using ?
.
Building on Vikram Deshmukh's answer here is a version that uses a varying cache-busting parameter and replaces it every time it is used.
Expanded version:
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);
})());
Compact version:
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); })());
Here is a useful answer for those wondering how to do this for 'mod_pagespeed', totally inspired by the above answers.
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");})());
Shorter version
javascript:((param = 'yourParam=yourValue') => {const loc = location.href; if (loc.contains(param)) return; location.href = loc + (loc.contains('?') ? '&' : '?') + param})()
Tested here and works but apparently doesn't work on some websites that use older JS like amazon.. Here's patible version:
javascript:(function (param = 'yourParam=yourValue') {var loc = location.href; if (loc.indexOf(param) > 0) return; location.href = loc + (loc.indexOf('?') > 0 ? '&' : '?') + param})()