使用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