How to invoking js function on click browser tab?
I am looking this solution for invoking user when session gets expire.
Not getting any Solution. Please Help.
If you want to detect, that the browser tab with your page was clicked, when user was on aother tab, then you can use window.onfocus
window.onfocus = function() { alert('Browser tab clicked'); };
Related
I am using a thirdparty tool that integrated in my html (PHP) as IFrame solution. (Recommendation of the manufacturer).
<iframe id="theIframe" src="manufacture.com"> </ iframe>
A pop-up is built up and redirect to a new popup with the login screen. Anyone has idea, how I get can capture all events from all popup. I need the reference from the login-screen or the close event of them.
How I can check or capture that this event.
The Iframe-initiated pop will be redirected to a second popup (for whatever reason) and will not bind the second popup to the iframe and will not get to the events.
if (window.attachEvent) {
window.attachEvent("onmessage", receiveMessage);
} else {
console.log("event message added");
window.addEventListener("message", receiveMessage, false);
}
function receiveMessage(event) {
//capture the event for closing the second popup
console.log(event);
alert("User has closed the window in the iFrame");
//Close iFrame
var iframe = document.getElementById('theIframe');
iframe.parentNode.removeChild(iframe);
}
}
In short, the problem is not really a problem.
There is no solution to this problem. I integrated the url of the second pop directly into the iframe. Sometimes the solution is so close.
Sometimes it just helps to rethink the problem.
I have the following code in my page to submit the form on the page automatically when the DOM is ready:
$(function () {
$('form').submit();
});
However, on the next page if the user clicks back on their browser it goes back to the page before this one rather than the page with this code on (with Chrome/IE anyway). i.e. the page with the form on is missing in the browser history.
This is great, although I wondered is this something all modern browsers now do? I am looking for an answer that cites official sources such as from internet standards documents or from browser vendors that state the mechanism they have implemented.
This appears to only happen if I call the submit() function in the DOM ready or Window load events.
e.g. this code will show the form page in browser history after the page is clicked (back/forward):-
document.addEventListener('click', function () { document.forms[0].submit(); }, false);
the following snippets won't:-
document.addEventListener('DOMContentLoaded', function () { document.forms[0].submit(); }, false);
window.addEventListener('load', function() { document.forms[0].submit(); }, false);
window.onload = function () { document.forms[0].submit(); };
I've dealt with this before. I did not want the back button to take
the user back to previous page. Using onbeforeunload solved the
issue for me...
But your issue is related to the following concepts
Browsing Context
Session History
Replacement Enabled (flag)
A "Browsing Context" is an environment in which "Document" objects
are presented to the user.
The sequence of "Document"s in a "Browsing Context" is its "Session History". The
"Session History" lists these "Document"s as flat entries.
"Replacement Enabled" comes into effect when we propagate from one "Document" to another in the "Session History". If the traversal was initiated with "Replacement Enabled", the entry immediately before the specified entry (in the "Session History") is removed.
Note A tab or window in a Web browser typically contains a browsing context, as does an iframe or frames in a frameset.
Logically thinking, by calling any of these
document.addEventListener( 'DOMContentLoaded', function() {document.forms[0].submit();}, false );
window.addEventListener( 'load', function() {document.forms[0].submit();}, false );
window.onload = function() {document.forms[0].submit();};
you are suggesting the browser to perform #3, because what those calls mean
is that propagate away from the page as soon as it loads. Even to me that code is
obviously :) asking to be cleared off from the "Session History".
Further reading...
onbeforeunload
browsers
browsing-context
unloading-documents
replacement-enabled
Since this code leaves the page in the history when responding to the click event:-
document.addEventListener('click', function () { document.forms[0].submit(); }, false);
and the following pieces of code do not leave the page in history (DOMContentLoaded, and window onload events):-
document.addEventListener('DOMContentLoaded', function () { document.forms[0].submit(); }, false);
window.addEventListener('load', function() { document.forms[0].submit(); }, false);
window.onload = function () { document.forms[0].submit(); };
it can be assumed that modern browsers do not record a navigation history for page navigation that occurs within the window load or document ready handlers.
When the user hits the back button, the browser shows the cached copy of the page. Form submit doesn't cache the page therefore it doesn't show up in your history.
Yes, redirecting from an onload event handler causes the new URL to replace the one you leave in the history (and thus doesn't add a useless entry). But that's not the only trigger for that replacement, it may also be caused by any location change occurring fast enough, this delay being designed to avoid polluting the history in case of JavaScript based re-directions.
It is very hard to find any specification on that topic but on Firefox this delay seems to be 15 seconds. Here's a mention of this delay in bugzilla from one of the moz developers :
Mozilla uses a threshold of 15 seconds to decide if a page should
stay in history or not. If a site uses and
redirects to another site with in 15 seconds OR redirects to another
page in onLoadHandler() etc ..., the redirected page will replace
(and thereby eliminating) the redirecting page from history. If the
redirection happens after 15 seconds, the redirecting page stays in
history.
One may argue about the
time limit. But this is just something we thought was a reasonable number
I want my javascript to be trigged when:
The current IE tab is switched out when multiple IE tabs are open.
When the current IE tab is closed.
I don't want my JS code be trigged by the in-page pops up dialogs.
When the whole IE window closed.
The lose focus event may not work for me because there are pop up diaglogs in my page, so when it pops out, the IE tab will lose focus, but since the tab is not switched or closed, I don't want my javascript to be trigged here.
Is there any solution? I am wondering if there's something like entering tab / leaving tab, or tab-switching events?
Some interesting links, but not resolve my question.
Is there a way to detect if a browser window is not currently active?
Hook into tab changed event of browser
if you use 'jQuery', you can easily do it .
$(window).blur(function(){
// your code
});
$(window).focus(function(){
// your code
});
here is the link which provides one more method to do it.
you may be interested in
(function(){
function doOnFocus(){ console.log("focus"); }
function doOnBlur(){ console.log("blur"); }
function doOnLeave(){ console.log("leave"); }
if('onfocusout' in document){
document.onfocusout = doOnBlur;
document.onfocusin = doOnFocus;
}else{
window.onblur = doOnBlur;
window.onfocus = doOnFocus;
}
window.onbeforeunload = doOnLeave;
})();
In javascript there is an event on window close it is not IE specific but is mostly used to call an alert before the user leaves the page. It's one of my pet peeves and is very annoying but may be what you are looking for.
window.onbeforeunload = yourfunctionthatexecutes;
I'm showing the pop-up window from the page. The popup has one page, which then redirects to another (LinkedIn authorization one, to be clear), and then, after the successful login, the initial authorization page is opened again.
I want to reload the parent page when the popup is closed, but cannot do this.
The code is the following:
function OpenAuthorizePopUp() {
var w = window.open("AuthorizePage.aspx", "PopUp", "width=450,height=540");
w.onunload = function () {
SubmitPage();
};
return false;
}
function SubmitPage() {
alert("SUBMIT!");
}
The issue here is that SubmitPage() function is called not when window is closed, but just after the popup is shown. I guess it's because of redirect inside the popup, and unload is raised when we move from the first page.
Is there a way to catch the actual moment when the window is closed in this case?
So the flow of the popup goes like this:
1. Your page -> 2. LinkedIn Page -> 3. Your Page
Can you change the URL that LinkedIn sends you back to (3.) ? If so, have LinkedIn send you back to a page that has window.onunload = window.parent.SubmitPage; on it.
If you can't- ie, LinkedIn always sends you back to the first page- make the third page test for a successful connection to LinkedIn and if it's present, execute window.onunload = window.parent.SubmitPage;.
A possible improvement to your idea would be to just have the "success" page call window.parent.SubmitPage(); and then window.close(); since you won't need the popup anymore.
Found the solution. Instead of accessing parent window with window.parent, wrote
opener.location.reload(true);
And it works.
I want to close a session in my application when user closes a window by pressing the close button in the title bar or using the Alt + 4 keyboard combo.
I am able to handle X using the code below, but this is not handling Alt + F4. I also tried onbeforeunload but this is not calling the function at all. I am testing with IE9.
<body onunload = "closingWindow()">
<script>
function closingWindow(){
if((window.event.clientX<0) || window.event.clientY<0)){
//Ajax call to close session
}
}
</script>
Check out window.onbeforeunload
https://developer.mozilla.org/en/DOM/window.onbeforeunload
Live Demo
window.onbeforeunload= function(){
// Ajax call to close session
}
Hit alt+f4 in the demo and you will notice the alert before it closes as well.