javascriptregexbookmarklet

Bookmarklet to add a parameter at end of each URL


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.net/ 

and with the bookmarklet I want to make the URL go to

http://subdomain.mysite.net/?clearcache

Same with deeper pages, as I would like it to go to

http://subdomain.mysite.net/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 becomes

http://subdomain.mysite.net/?clearcache//subdomain.mysite.net/

I feel I am close to it, but I just need a small tip from experts. I hope for a a reply. Thanks


Solution

  • This 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 ?.