Close browser tab - javascript

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.

Related

Tampermonkey script --After SWITCHING to a new tab from an old one, push an alert to the new one

I'm running a tampermonkey script on chrome.
What I want:
If a user starts off at an old tab, say google.com, and they go to another tab, I want to alert the user something.
Problem is, my alert currently only pops up on the google tab (THE OLD TAB), but now the user isn't on the google tab, they're on the other tab.
So they don't actually see the alert pop up, making it useless. Is there a way to pop the alert on the tab currently in focus (aka the new tab you navigated to from the google tab) or on all tabs in the browser window? Also, the solution will have to NOT include matching all possible url's on the internet.
IF the solution pops an alert on all tabs in the browser window, it should also be that clicking "okay" will dismiss all the alerts in each tab).
I've been using the window.onblur function to know whether the user switches tabs from the old tab to another, and it fires. Again, the problem is the alert pops up on the tab that the user does not see
$(window).on('blur',function () {
alert("You switched tabs");
// do some stuff after tab was changed e.g.
})
You can communicate between tabs with GM_setValue() and GM_getValue(). You can set your current url as value on blur and run a loop (with setInterval()) to get the value and look if it changed/alert based on its content.
It is not possible to dismiss alerts in other tabs.

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.)

IE11 alert after navigation begins cancels the navigation

We have some Javascript that is used for navigation when you click a tab in which the window.location is set to another URL with query string parameters. This URL basically:
checks if the user is logged in
If they are not, redirect them to an error page to login again
If they are, logs them out and continues to the next step
redirects them to another url of another system which then logs them in to that system
If the next system is taking some time completing the request, the browser is sitting there with the loading icon, and the user may get impatient and click the tab again. If they do, step 1 is done a second time, and now that they are logged out from the initial processing of step 1.2, they fail the check and end up following the path of 1.1.
The workaround we put in place is pretty simple. We put the window.location into an IF statement that checks if a person has already clicked on a tab and navigation has already been kicked off. If not, then it does the window.location setting. If so, then we call "alert('Please be patient');". This seems to work fine for every browser except for IE11, and even then, only when F12 dev console is not open. If you have it open, it works fine.
But if you don't, for some reason the alert popup ends up cancelling the navigation, and the page never goes anywhere. Almost the same as if you pressed escape, or clicked the "Stop" button. There's also sometimes this weird scenario where the next page loads, but it thinks that the alert window is still open, but it's not. I can see the extra task in task manager, but it's not visible.
Has anybody heard or seen anything like this? Please note, I know there are other ways of handling this situation other than using an alert. I'm not looking for suggestions about how to accomplish a message in a different manner. I'm more interested in finding out if there's any decent documentation that could explain to me why an alert after the page has started to unload may be behaving this way in IE11, and only when F12 isn't open.
EDIT Adding code Example
var navigatingTo;
function doNavigation(destination){
if (navigatingTo && navigatingTo != ""){
alert(navigatingTo);
} else {
navigatingTo = "Please be patient\nNavigating to " + destination;
window.location = "/somepage.aspx?d=" + destination;
}
}

how to keep only one tab or page for every user in the browser?

I'm writting a real-time chat application.
And I want to keep only one tab in the browser(chrome)
(firefox seems to act like that).
I've tried using cookie..
below is the js code using jquery.cookie.js
if($.cookie("online")){
window.close();
}else{
$.cookie("online",1);
}
But I did't get what I want.After refreshing the page will terminate the tab anyway.
and the cookie will stay alive until the browser is closed but not the tab.
Can anybody help me?
You cannot programaticaly close the current tab unless you opened it with Javascript.
This would also be very confusing for visitors, as they would get no information as to what happened, the window would simply close on them when they tried to open it.
Instead, consider displaying an error message or splash page telling them why they are not getting a second copy of the chat room, and telling them to return to the previous window. This would be much more user friendly.

Use Javascript Alert To Keep Users On A Page

I am creating an internal web based application that will not be the target audience of the web.
I understand the frustration of alert boxes and forcing people to do certain things.
With that said, what I am attempting to do is create a javascript function, that unless a user clicks a link on a specific page, if they try to navigate away from the page other than using a link on the page, I would like to alert them and say, sorry you need to click the appropriate link to exit.
What my issue is, is that I need to lock out fields, and what I can do when a user hits an edit page, im going to write to a table that user to the lockoutuser colum. If a value exist, that user can access the record if it is null, it means no one is editing the record. If someone clicks to go into that record they lock it out, my means of updating the lockoutuser colum could be ajaxy on unload of the page, but the page could be unloaded for 2 reasons, 1 the edit form is submitted or the user leaves the page.
An alert that would say, sorry you can leave this record without clicking the big red button that says unlock, and force the user without refreshing to stay on the page.
I understand the machine could crash and or an alt f4 or a brute end task on the browser will still leave me other work to unlock the record
You need to use the onunload event of the page to present a messagebox when the user tries to leave your page. Check out this example: http://www.codetoad.com/javascript/miscellaneous/onunload_event_eg.asp

Categories

Resources