Switch/alert browser tab with JavaScript - javascript

I have a warning which users need to respond to on my site. Ideally I'd like to switch their tab in a similar way to how the alert() function currently does but without the alert popup.
If this isn't possible is there any way to make the tab flash to show a user response is needed?

One thing you could do to not spam the users watching your page/on your tab but work with your flow is this.
JavasScript
Function WarnUser ()
{
//If user isn't on this page alert them to problem.
if (!document.hasFocus())
{
alert("Warning!");
}
//Create pop up to deal with warning
}
I know this uses an Alert but your dialog would resolve the issue, the alert just brings them to the tab having a problem. Also with !document.hasFocus() it would only alert them when they are somewhere else which removes some of the redundancy.
Alternatively if a prompt dialog could resolve your warning that would also have a similar affect.

Related

How to close a popup alert which may appear any time while browsing the website in cypress?

While browsing the website an email popup alert may appear , how can I close it whenever it appears (the problem I don't know when it may appear)
if (cy.get('.popup').isDisplayed) {
cy.get('.popup a').click() // Close button
}
To close whenever requires some sort of watcher, which doesn't quite fit the Cypress pattern.
Best approach would be to cy.stub() it so it never occurs. What type of popup do you have, what triggers it?
If it's timer based, you might be able to cy.clock() to freeze the clock. If you need to cy.tick(milliseconds) during the test, then follow it immediately with
if (Cypress.$('.popup').length) { // non-failing check if popup is present
cy.get('.popup a').click()
}

window beforeunload is not working as expected in jquery

Basically what i am trying to do was, whenever a user tries to close the current tab(when he was on my site), i want to display a pop up with three choices about why he was leaving and want to store that choice some where
So i have written the following in main.js which will be loaded through entire site pages
$(document).ready(function() {
// Before closing the current tab, ask user for a reason
$(window).on('beforeunload', function(event){
$('#load_choices_up').click();
event.stopPropagation();
event.preventDefault();
debugger;
});
});
So i have three issues with the above jquery code
*.This code was executing even when i click another link on the same page(I mean if i navigate to another page from current page), but i only want this code to run when the current tab/page was closed(about to close) completely, but not when navigating to another page on my site
*. After this line $('#load_choices_up').click() was executed, a choices pop up was opening as expected, but immediately the default processing of browser(that is closing functionality) was not being stopped with two lines event.stopPropagation(); and event.preventDefault();, i mean these two methods of stopping the behaviour is not working and the browser is closed, but i want to do some processing based on user choices input and then based on that i will close tab.
*. When i used return "Why do you want to leave the page", instead of choices pop up, the browser was displaying different message based on browser type like "You have unsaved changes" in chrome, and some different message in firefox, instead of displaying my custom message
So finally, why event.stopPropagation(); and event.preventDefault(); are not working ? and why i can't able to display my custom message ?
You can't prevent someone from closing the browser. This is for obvious security reasons. Imagine a spam-website preventing you from closing the website while pumping you full of god knows what.
You can at most pull one function like an alert() or a prompt. After a user closes them, the tab will close either way.
beforeUnload is also extremely short-timed. You won't be able to run massive scripts with it, as the user would probably close the tap before any script would run properly. (I tried it with an ajax call, didn't work)
So, even if you're able to get the options you want in there, the moment a user chooses one of the options, you're not going to be able to save it anywhere. Your script will never make it that far.
You can customize the "are you sure?" message like so:
$(document).ready(function() {
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
});
but again, you can only change the text. It's a browser's native functionality, and you cannot change it.

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 force alert window to pop-up

After several time showing alert window the browser ask whether to prevent creating additional dialog.
Is there a way to force JavaScript show alert window even the user has checked the 'Prevent this page from creating additional dialog' ?
No, there isn't.
Thank goodness.
(you could, however, fake it with a modal dialog--possibly jQuery UI would suit your needs)
The whole point of that checkbox is to prevent sites from continuously spamming the user with alert boxes, preventing them from accessing anything in their browser anymore, even switching tabs. If it were possible for a site to continue posting new alert messages after the user decided he doesn't want to see it anymore, the whole buisness of "Don't click this link" where you'd end up on an infinite loop of alerts if you did click it, would be made possible again. Nobody wants that.

Beforeunload is not working to redirect user to another page, how can I redirect user to another page/url when he attempts to close a window/tab?

My following code isn't working to redirect the user to another page:
$(window).on('beforeunload',function(){
window.location.href="http://www.google.com;
}) ;
I want the user to be redirected to another page when he attempts to close the tab.
What's the alternative and appropriate way to achieve this?
*Don't do it*
But it is possible with the user's permission; you can achieve something like this (took me a while to find a website that was happy in a frame)
window.onbeforeunload = function () {
window.setTimeout(function () { // escape function context
window.location = 'http://bbc.co.uk';
}, 0);
window.onbeforeunload = null; // necessary to prevent infinite loop
// that kills your browser
return 'Press "Stay On Page" to go to BBC website!';
// pressing leave will still leave, but the GET may be fired first anyway
}
Demo
I don't think this is possible.
There are some things you can can do in this event but that is severely limited due to spammers back in the day. They used to have animated text in the window statusbar which would obscure link href's so you would be clicking blind and open tons of windows when you tried to leave so that you were essentially trapped on the site.
This got to be such a problem that as far as I recall it was one of the "features" that Firefox bragged about solving when it first launched.
It was toned down to being able to beg them to stay with a dialog box but then that was abused as people worded it like official system messages and tricked people.
Now most browsers let you request a "stay on page / leave page" dialog but dont give you any control over the wording.
Here are some docs that list your options:
https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload

Categories

Resources