javascript should run only if tab/browser window is focused [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detect If Browser Tab Has Focus
I have a simple java applet that captures client's screen. With the help of a piece of javascript code, I am able to call the applet and capture the active screen picture.
But, even though it only captures active screen with a click on a button, users are likely to manipulate the process by switching to some other tab with ALT TAB while capture process. I want to make sure that capturing must be done only if page is loaded and page is focused.
So far, I found this piece of javascript code which doesn't seem to be working correctly. Sometimes it gets stuck at the focus even though the page is minimized.
<script language="javascript">
window.onpageshow = function(e) { console.log('pageshow'); };
window.onfocus = function(e) { console.log('focus'); };
</script>
So are there any suggestions to what I am trying to achieve or any other solutions.

Try using document.hidden it's part of the page visibility API

Related

Is there a way to simulate the action of closing a browser's tab/instance using Protractor/Selenium? [duplicate]

This question already has answers here:
How to disable a "Reload site? Changes you made may not be saved" popup for (python) selenium tests in chrome?
(2 answers)
Closed 3 years ago.
I am trying to automate a scenario in which the User closes the browser's tab or the browser itself. The User is expected to be prompted with an alert when doing so along the lines of Are you sure you want to leave?
The alert is displayed when I manually close the tab, or the browser. However, when attempting to automate it via browser.close(), or browser.quit() or executing a script window.close(), the alert is entirely ignored and the browser shuts down.
Is there a way to simulate the action of a closing the browser's tab?
I'm using: Protractor / Google Chrome
Here are some things you can try:
1) You can open a new window handle and then switch to the old one and close that one.
let url = https://google.com;
return browser.executeScript("return window.open(arguments[0], '_blank')", url);
browser.getAllWindowHandles().then(function (handles) {
browser.driver.switchTo().window(handles[0]);
browser.driver.close();
browser.driver.switchTo().window(handles[1]);
});
2) If the above command forces the browser to close, you should be able to target the cross button that closes the browser by using actions and moving the mouse cursor there.
3) You can try using the keyboard command for quitting the browser using sendKeys Control + q.

How does stackoverflow create the modal dialog window? [duplicate]

This question already has answers here:
Modal Dialog without jQuery
(7 answers)
javascript to check when the browser window is closed
(5 answers)
Closed 7 years ago.
When I edit a page and try to leave it I get a dialog box that appears and asks me if I want to:
Leave this Page or Stay on this Page
The dialog blocks me from doing anything until I have answered.
In Javascript the only similar thing I see is window.confirm(message) which gives and OK and Cancel button.
So is there another way in Javascript that I can create a dialog that I don't know about? Please note that I want to create a dialog at any time and I am not interested in knowing when the page closes so this is not a duplicate of the question identified by Zeta.
What happens here, is not an alert() or confirm() or prompt() of any sort.
Write a function for the window.onbeforeunload event handler, load up the parameter with a function passing a message...
➪ ➪ ➪ stackoverflow.com/a/1119324/444255
What it generates is a special native dialog by your browser. (You can´t inspect it with devTools, it's not made from HTML... ok, alerts are neither.)
You can't get the same dialog for other purposes. (If it was possible, the advertising industry would habe let us known long ago ;-)
You can only 'block' either with ugly alert's and it's siblings, or by putting a shim over your page (to prevent clicks) and a html-made dialog on top. Which should stop other interactions with your page, but of course not navigating away.
JavaScript doesn't have a built in modal, only alert, confirm and prompt. Many libraries have made their own modals, Bootstrap and jQueryUI for example.
Your question isn't quite clear. What I understand is that you're interested in the available models.
window.alert() gives you the chance to send a notification to the user.
window.confirm() gives a popup with an Ok and a Cancel button.
window.prompt() gives a popup in which the user can insert text.
http://www.w3schools.com/js/js_popup.asp

Notification When Chrome Exits [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Event onBrowserClose for Google Chrome?
Is it possible to detect when the user exits the Chrome browser, so I can process some data right before they exit? I have looked into using chrome.windows.onRemoved.addListener(function(integer windowId) {...}); but it only listens for a window and not the entire browser.
Well, no.
There is no Close event, and Chrome doesn't guarantee that all others pending events will be fired before closing (will be fired at all).
I personally researched this topic while writing the "History Eliminator" extension, that would erase your browser history on close.

How to trigger jQuery when the browser is closed [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript detect browser close tab/close browser
Does anyone know a reliable way to listen out for a window closing event in javascript/ jQuery?
The window is the parent and not any child instances. I.e. if a window is closed by mistake and the visitor launches their browser again and loads the url previously visited once more.
You can use the window.unload event to set a cookie or use local storage to save the time using new.date(), then see if the visitor returned within a set amount of time.
Something like:
$(window).unload(function() {
localStorage.setItem(“theyLeft”, new Date());
}
then on load check for :
$(window).load(function() {
var timeGoneBy = new Date() - localStorage.getItem(“theyLeft”);
//calculate time gone by, and do something if visitor returned within given time etc.
}
Would need to be refined a lot, and local storage should have cookies as fallback, but just to show the jist of it.
Try the unload method.
The unload event is sent to the window element when the user navigates
away from the page. This could mean one of many things. The user could
have clicked on a link to leave the page, or typed in a new URL in the
address bar. The forward and back buttons will trigger the event.
Closing the browser window will cause the event to be triggered. Even
a page reload will first create an unload event.
You can also try playing with the JS onunload and onbeforeunload events.

How to alert user to not close the window while he has submitted the form to process? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript To Get An Alert When Closing The Browser Window
I am having a site on which users are visiting and uploading video, in that i am having a problem that users are closing the browsers or navigating from the page without letting the upload process to complete, i want to show a alert box to the user to not close the window or not to navigate from the page when uploading process is going on, i tried it through window.unload but it will not work for me because as the uploading process is going on the page will be submitted so that event will always be called, please tell me the way to show the alert box to users on window close or they navigate from the page.
Thanks in advance,
Ravinder Singh
Did you try to use:
window.onbeforeunload

Categories