使用Do something only once lib从Mozilla的:
function executeOnce() {
var argc = arguments.length,
bImplGlob = typeof arguments[argc - 1] === "string";
if (bImplGlob) {
argc++;
}
if (argc < 3) {
throw new TypeError("executeOnce - not enough arguments");
}
var fExec = arguments[0],
sKey = arguments[argc - 2];
if (typeof fExec !== "function") {
throw new TypeError("executeOnce - first argument must be a function");
}
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
throw new TypeError("executeOnce - invalid identifier");
}
if (decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) === "1") {
return false;
}
fExec.apply(argc > 3 ? arguments[1] : null, argc > 4 ? Array.prototype.slice.call(arguments, 2, argc - 2) : []);
document.cookie = encodeURIComponent(sKey) + "=1; expires=Fri, 31 Dec 9999 23:59:59 GMT" + (bImplGlob || !arguments[argc - 1] ? "; path=/" : "");
return true;
}
您可以进行以下操作:
// some custom helper
String.prototype.s2b64 = function() { // make base64 from string
return window.btoa(unescape(encodeURIComponent(this)));
}
String.prototype.replaceArray = function(find, replace) { // replace by array
var replaceString = this;
for (var i = 0; i < find.length; i++) {
replaceString = replaceString.replace(find[i], replace[i]);
}
return replaceString;
};
// here we start :
var output = [];
$.each($("#urlslist textarea").first().val().split(/\n/), function(index, value) {
alert(index + ": " + value);
if (value != '')
executeOnce(function(v) {
output.push(value);
}, null, value, 'urldone_' + value.s2b64().replaceArray(
["expires", "path", "domain", "secure"],
["e_xpires", "p_ath", "d_omain", "s_ecure"]) + '');
});
alert(output);
有些部分可能看起来有点古怪,但它是所有关于牛市防止手动setCookie(如使用B64和搜索&取代,如果我们真的很倒霉并随机从b64输出中获取cookie关键字)
如果要删除所有保存的值,请在此处是代码:
var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
// Remove any that match the pattern
for (var i = 0; i < cookieNames.length; i++) {
if (/^urldone_/.test(cookieNames[i])) {
document.cookie = cookieNames[i] + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
}
}
JSfiddle
这是一个基于cookie的答案(由浏览器,刷新&重启证明)。
你想阻止发射,而第一个请求“的道路上”第二个请求?如果不是,你能解释一下“旧网址”的含义吗? – dimlucas
禁用您的提交按钮,一旦点击并在获得响应或错误后再次启用 –
@dimlucas检查我的编辑 – user2650277