Cookie is not deleted on browser close? - javascript

Scenario:
For example: Anonymous User enters my website xyz.com, I have to show a popup "Hey welcome", If user opens a new tab without closing the browser and enters xyz.com then we should not show the popup again. If the user closes the browser and come back again to xyz.com then we have to show "Hey welcome".
Note:
No server-side involved. Need a client side solution.
What I tried:
I have tried cookie without expiry
document.cookie = "welcome=true";
But cookie is not deleted on close of browser.
No, luck with onbeforeunload and unload Since event is triggered on page refresh
Any idea or suggestion will be grateful to solve this issue.

Related

How to make a link so that clicking it fires a GET but do not open up a window

In my current implementetion of two-factor auth, I'm sending a SMS to the user with a link. When the link is clicked, the login proceedes on the users machine.
However, on the phone the browser will open up a empty page (since I'm currently returning 204 from the webserver for the link clicked in the SMS.
What I would like to happen is that when the user clicks the link in the SMS, the broser is not opened at all. It should just send the GET request, and the user will not have to close the browser window on his phone.
If that is not possible, is it possible to make the window that get's opened to autoclose imediately?
The browser has to open to make the request. There's nothing you can do about this.
However, if you control the content from the web server, you can attempt a window.close() on-load. Realistically, you'll probably get blocked on this and should also have a fallback content that says something like, "logged in, please close this window". (You can probably call window.close() on a button click.)

Close browser tab

I have a page that tells users to go to their email and click verification link. When they do, the link in the email message opens a new tab, so now the user has two tabs open, both related to email, which is a bit confusing.
Is there any chance to have them click the link in their email and open confirmation within the first tab or
Open the new tab but at the same time close the original tab?
There's gotta be way with JS, I hope.
No, but you could run a Javascript in your first tab that pings your server every 10 seconds or so, to check if the account was confirmed.
If it was, then the page redirects itself to the new user's dashboard (or some other "main account page").
setInterval(function(){
$.get('/api/is_confirmed.php').then(
function(response){
if (response.status_code == '200')
window.location = '/dashboard.php';
}
);
}, 10000);
I would advise against self.close()ing the window. Do not interfere with the user's control of the browser tabs. Having tabs suddenly disappear by themselves is unexpected for the user and annoying at best, frightening at worst.
You can check periodically from the 1st tab if the email got activated then close it window.close() , thus the remaining open tab would be the 2nd one, typically, you would implement something like that using setInterval and an ajax request to your server backend to check if the email was activated or not yet.
One thing you could do is:
1- Instead of having a link to your page in that email, put a link to a special page that changes a database value then closes itself.
2- On the main page, have a thread constantly poll that database value and as soon as you see the change, update the page with ajax (or refresh) then bring the broser on top of other windows.

Clear session on window close

I am working on a examination project, When user starts a test it open in new window by window.open(). If user close a test before it finish then session creates a problem so I want to clear session when user close the browser window or hide/disable close button before test completes.
nothing you come up with here will not be overrulable by the user; welcome to client-side programming (the actual browser "close" button is not within your reach). You can tap into onclose (lots of sites do this in order to pop up a confirm("do you want to leave this page?") dialog) but even that is very easy to bypass by anyone who knows how to open a browser console (F12 on every browser, for example) because they can just redefine window.confirm = (() => true); and not get bothered by confirm dialogs.
Partial solution would be to use the onbeforeunload and send a request back to the server once it is invoked.
Once the request is received on the server, you can destroy the session.
Again, this is easy to bypass on the client side but does provide extra functionality.
window.onbeforeunload = function() {
// AJAX to server
};
You could use this answer here:https://stackoverflow.com/a/1119324/1489237
That would give you that message "are you sure you want to leave this page?"
On that function,instead of a message you can submit an ajax that will call a script to do a session_destroy() before closing the page.
I think you can set session expiration time to be few seconds? and perform ajax requests to maintain it, while student is answering questons
Can you use popup block with iframe inside?
<div id="test-popup">
<iframe src="/test/12345">
</div>
And open it with JS:
$('#test-popup').show();
This is for example only. You need more CSS and JS to make it works.

Disable browser back button for a CLIENT_CERT auth webapp

My webapp has a CLIENT_CERT based JAAS authentication. I am using IE7.
When I click on logout, it takes me to my home page. Now clicking on the back button, the user should remain on the same page, which I acheived using history.forward() javascript. But the certificate dialog comes up since the previous page was secured.
How can I avoid the certificate dialog from not coming and also remain on the non-secure home page when user clicks on back button after logging out.
The only way to disable the back button within a window is to use location.replace() for every single interaction, which you cannot do if you need to submit any forms unless you target them to a hidden iframe and then do a location.replace() in reaction to the iframe's onload event once the form is submitted. This is really nasty and complicates everything.
The other technique to avoid users going back through pages (some online banking sites do it this way) is to launch the secure section in a new window, and have logout close it (you can force a close in IE with window.opener = null; before window.close();.

want to detect browser close event?

I am working on any application in which i need to detect that whether user close the tab or browser so I can disconnect the user from other user basically its an chat application.
I have used :-
window.onbeforeunload = confirmExit;
function confirmExit() {
if(needToConfirm) {
return "Leaving this page will end your conversation.";
}
Its work fine, when I try to close the browser or tab it shows an popup with message "Press OK to continue, or Cancel to stay on the current page."
I want to perform task when user click on Ok button and if he press cancel then will stay on current page.
Please Help me :(
Thanks in advance
Ansh J
This is not possible.
You could handle the unload event and send an AJAX request there, but that might not be completely reliable.
The best solution is to send an AJAX hearbeat every x minutes, and consider the user disconnected if you stop receiving the heartbeat.
Go the other way around: when you receive the Unload event, send the server a message that informs the user is about to disconnect. The server waits for some time and then automatically disconnects.
Then, if the user click cancel and stays on the page, you send a message to the server to inform that the client is still alive.
The downside is that, if the user waits too long to click cancel, he might be disconnected.
Why not have the client's periodically 'ping' the server to let it know that they are still there, and if a user misses say 3 pings then it will be marked as logged off?

Categories

Resources