Skip to content

Instantly share code, notes, and snippets.

View rajkundalia's full-sized avatar

Raj Kundalia rajkundalia

View GitHub Profile
@rajkundalia
rajkundalia / SingleWindowCheckUsingCookie.js
Last active August 17, 2021 10:20
The approach for limiting user to single window using document cookie
/**
* This function checks if multiple tabs for the application are not using the document.cookie.
* The approach here is as follows:
* 1. Fetch the cookies and split it and then fetch the value for the cookie name i.e. tabNumber.
* 2. We check if the cookie is value for tabNumber is > 0, for the first time it won't be and we go to the else part
* where we set the value to be tabNumber=1. The tab would load successfully.
* 3. Now when we open the second tab in the same browser session, the cookie will have tabNumber=1, we extract the value
* and we check if cookieValue > 0 and we show an alert saying second tab was opened.
* 4. The line window.open(window.location, '_self') was added so that we get redirected using the same URL and we don't
* let the user open the tab anyhow (my main requirement).
@rajkundalia
rajkundalia / SingleWindowCheckUsingSessionStorageAndLocalStorage.js
Last active August 18, 2021 06:33
The approach for limiting user to single window using local storage and session storage
function createGUID() {
let guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
let r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return guid;
}
/**