मैं जानता हूँ कि इस पोस्ट बहुत पुराना है, लेकिन मामले में यह किसी को भी मदद करता है, मैं हाल ही में मूल रूप से एक ही बात localStorage और sessionStorage का उपयोग कर कर में देखा।
इसी तरह के Anthony's answer, यह सुनिश्चित करने के लिए एक अंतराल सेट करता है कि मूल टैब प्रविष्टि को ताजा रखता है, ताकि अगर ब्राउज़र क्रैश हो या अनलोड ईवेंट को कॉल किए बिना किसी भी तरह बंद हो जाए (टिप्पणियों में शामिल है लेकिन परीक्षण उद्देश्यों के लिए कोड का हिस्सा नहीं है), तो एक नई ब्राउज़र विंडो में एप्लिकेशन ठीक से चलने से पहले बस थोड़ी देर हो जाएगी।
जाहिर है, आप "टैब अच्छा है" बदल देंगे, "टैब खराब है" जो भी तर्क आप चाहते हैं उसे करने के लिए।
ओह, और साथ ही, createGUID विधि सत्र पहचानकर्ता अद्वितीय बनाने के लिए केवल एक उपयोगिता है ... यह this answer से पिछले प्रश्न पर है (यह सुनिश्चित करना चाहता था कि मैं इसके लिए क्रेडिट नहीं ले रहा था)।
https://jsfiddle.net/yex8k2ts/30/
let localStorageTimeout = 15 * 1000; // 15,000 milliseconds = 15 seconds.
let localStorageResetInterval = 10 * 1000; // 10,000 milliseconds = 10 seconds.
let localStorageTabKey = 'test-application-browser-tab';
let sessionStorageGuidKey = 'browser-tab-guid';
function createGUID() {
let guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
/*eslint-disable*/
let r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
/*eslint-enable*/
return v.toString(16);
});
return guid;
}
/**
* Compare our tab identifier associated with this session (particular tab)
* with that of one that is in localStorage (the active one for this browser).
* This browser tab is good if any of the following are true:
* 1. There is no localStorage Guid yet (first browser tab).
* 2. The localStorage Guid matches the session Guid. Same tab, refreshed.
* 3. The localStorage timeout period has ended.
*
* If our current session is the correct active one, an interval will continue
* to re-insert the localStorage value with an updated timestamp.
*
* Another thing, that should be done (so you can open a tab within 15 seconds of closing it) would be to do the following (or hook onto an existing onunload method):
* window.onunload =() => {
localStorage.removeItem(localStorageTabKey);
};
*/
function testTab() {
let sessionGuid = sessionStorage.getItem(sessionStorageGuidKey) || createGUID();
let tabObj = JSON.parse(localStorage.getItem(localStorageTabKey)) || null;
sessionStorage.setItem(sessionStorageGuidKey, sessionGuid);
// If no or stale tab object, our session is the winner. If the guid matches, ours is still the winner
if (tabObj === null || (tabObj.timestamp < new Date().getTime() - localStorageTimeout) || tabObj.guid === sessionGuid) {
function setTabObj() {
let newTabObj = {
guid: sessionGuid,
timestamp: new Date().getTime()
};
localStorage.setItem(localStorageTabKey, JSON.stringify(newTabObj));
}
setTabObj();
setInterval(setTabObj, localStorageResetInterval);
return true;
} else {
// An active tab is already open that does not match our session guid.
return false;
}
}
if (testTab()) {
document.getElementById('result').innerHTML = 'tab is good';
} else {
document.getElementById('result').innerHTML = 'tab is bad';
}
वैसे आप एक छोटे जावास्क्रिप्ट हर अब और फिर एक दिल की धड़कन अनुरोध पोस्ट वर्तमान उपयोगकर्ता के सत्र आईडी के साथ, इससे पहले कि पृष्ठ लोड आप जाँच लें कि यह ajax दिल की धड़कन में आ रहा है हो सकता था इस सत्र के लिए, यदि यह है, तो आप इसे समाप्त कर देते हैं। – gideon
आप इसे सीमित मामलों के लिए ऐसा करने में सक्षम हो सकते हैं, लेकिन आम तौर पर यह असंभव है।उपयोगकर्ता कुछ या सभी स्क्रिप्ट अक्षम कर सकते हैं, कुकीज़ और इतिहास तक पहुंच से इनकार कर सकते हैं, और कुछ ब्राउज़र एक टैब में पूरी तरह से नया सत्र शुरू करने की अनुमति देते हैं, इसलिए आईपी स्नीफिंग (जो सर्वर आसानी से कर सकता है) के अलावा आप भाग्य से बाहर हैं। – RobG