Bookmarklet在每个URL的末尾添加参数

时间:2013-12-19 09:37:54

标签: javascript regex bookmarklet

我正在尝试使用一个简单的书签,在任何网址的末尾添加一个简短的参数

我使用?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/

我觉得我很接近它,但我只需要专家的小小提示。 我希望得到答复。 感谢

3 个答案:

答案 0 :(得分:5)

这可以解决您的问题:

<强>代码

javascript:void((function(){var loc = location.href; loc.indexOf("?") == -1 ? (location.href = loc+"?clearcache") : (location.href = loc+"&clearcache");})());

<强>解释

检查是否存在任何其他查询字符串参数。如果是,请使用clearcache在最后添加&,或使用?附加到网址。

答案 1 :(得分:0)

对于那些想知道如何为'mod_pagespeed'做这件事的人来说,这是一个有用的答案,完全受到上述答案的启发。

.modal-backdrop {
    opacity: 0;
}

答案 2 :(得分:0)

此版本基于Vikram Deshmukh's answer,该版本使用不同的缓存清除参数,并在每次使用时替换它。

扩展版本:

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); })());