Javascript not working in Chrome - javascript

window.onbeforeunload = function() { logout();};
function Logout()
{
window.location.href = "logout URL";
return false;
}
My requirement is: when user clicks close button, user should be logged out. It is working fine in IE. But not working in Chrome.
Can any one please suggest.

Your function name is Logout() not logout(). Case Sensitivity

Try,
window.onbeforeunload = logout;
function logout() {
window.location.href = "logout url";
return false;
}
UPDATE:
First is to check whether logout function is ever getting called by putting alerts in
logout function or if you are using newer versions of chrome then use console.log('called').
You can try this alternative to check:
window.onbeforeunload = function() {
logout();
return null;
}
function logout() {
window.location.href = "logout url";
}

logout() !== Logout()
Try the below one
window.onbeforeunload = logout;
function logout()
{
window.location.href = "logout URL";
return false;
}

Related

Navigate stop only when going to another website

I am using following code to trigger the are you sure leaving website alert but for some reason its not recognising my if else condition in it and only works if I only put return true in window.onbeforeunload = function() { return true } . Is there a way I can trigger this alert only when user is navigating away from my website cause at the moment without if else condition its asking if user tries to navigate in the same website as well?
window.onbeforeunload = function() {
var location = window.document.activeElement.href;
if (typeof location != 'undefined')
{
console.log(location);
} else { reutn true; }
};
You can set a flag and toggle that flagged based on host of links that are clicked
var host = location.hostname,
allowNavigate = false;
window.onbeforeunload = function() {
if (!allowNavigate) {
return 'Message string';// not what actually gets displayed in most browsers these days
}
//don't return anything
return;
};
window.onload = function() {
document.querySelectorAll('a').forEach(function(a) {
a.addEventListener('click', function(e) {
allowNavigate = this.hostname === host;
});
});
};
The hostname on this page for example is "stackoverflow.com"
DEMO
You can add the "window.onbeforeunload" dynamically for the links you want to see the prompt message
and remove the "window.onbeforeunload" for the links you don't want prompt
<a onClick="a(true)" href="https://www.w3schools.com">Click here to get promt before navigate</a>
<br>
<a onClick="a(false)" href="https://jsfiddle.net/">Click here to navigate without promt </a>
<script>
function a(showPrompt){
window.onbeforeunload = showPrompt ? function(e) {return '';}: null;
}
</script>
https://jsfiddle.net/vqsnmamy/1/

prevent redirects if not on same domain (javascript)

Is there a way to prevent redirects if they aren't on the same domain? There's this snippet that works well:
window.onbeforeunload = function() {
return true;
}
But the problem is that it prompts user on every redirect. I would like to truncate prompts only to different domain redirects. I've tried:
window.addEventListener('beforeunload', function(e) {
if (!location.href.match('domain')) {
window.onbeforeunload = function() {
return true;
}
}
and
window.onhashchange = function() {
if(!location.href.match('domain')) {
window.onbeforeunload = function() {
return true;
};
}
}
and plenty of other variations but they don't seem to work. Did anyone have a similar problem?
Try with window.location's property hostname:
window.onbeforeunload = function() {
return window.location.hostname !== 'example.com'; // your domain
};
Long story short: using JS it isn't possible to know what the next adress is so it can't be done due to privacy policy.

How to relocate to another page when the user refreshes the browser?

I'm looking to have the following functionality. When the user refreshes the page I want it to go to a certain page Eg:
$(window).bind('beforeunload',function(){
window.location.href = "#splash";
});
Now the .bind() works because if I say return 'Really?'; it opens up a dialog saying Really? But I would like the page to be changed. How would I do this?
i have found next tip (tested in latest chrome):
var flag = true;
function confirmExit()
{
if (flag)
setTimeout(function(){flag=false;location = '/index.html';});
}
window.onbeforeunload = confirmExit;
Try this one.
$(window).bind('beforeunload',function(e){
e.preventDefault();
window.location.href = "#splash";
});
Use:
function confirmExit()
{
alert("exiting");
window.location.href='index.html';
return true;
}
window.onbeforeunload = confirmExit;

window.onbeforeunload executed on page refresh instead of on page close

I'm using window.onbeforeunload to pop up a confirm dialog when a close event occurs, but the confirm dialog appears on page refresh and doesn't execute on page close.
Here's the JavaScript code:
<script language="JavaScript">
window.onbeforeunload = confirmWinClose();
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
</script>
I tried it on Chrome, Firefox and Internet Explorer.
PROBLEM WITH YOUR CODE:
the function will be called when you refresh, because on refresh the page is unloaded and then reloaded.
in your solution, you should also note that you are not assigning a function to window.onbeforeunload but you are assigning the return value of the function when you write
window.onbeforeunload = confirmWinClose();
which might also execute the function (based on where you place it in the javascript) whenever the assignment is done. For e.g.
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
window.onbeforeunload = confirmWinClose();
the above code will execute the confirmWinClose function whenever this js is loaded.
(not your case as you have defined the function after call, so won't be executed on load, but you should remember this)
SOLUTION:
the below solution is working for close also
instead of your solution, i tried this
JS:
window.onbeforeunload = function() {
var confirmClose = confirm('Close?');
return confirmClose;
}
or
window.onbeforeunload = confirmWinClose; //note the absence of calling parantheses
function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}
this works as expected.
also note that you should return from the onbeforeunload explicitly.
even if you have to call a function, you should do
<script>
window.onbeforeunload = function(e) {
callSomeFunction();
return null;
};
</script>
No full solution, but you can intercept the F5 key (not intercepted if the user click on the refresh browser button...)
var isRefresh = false;
// with jquery
$(function() {
$(document).on("keydown", function(e) {
if (e.which === 116)
{
isRefresh = true;
}
});
});
window.onbeforeunload = function() {
if (! isRefresh)
{
return confirm ('Close ?');
}
return false;
};
Since you anyway only want to display your text, What just using the onbeforeunload as it is expected just return the string?
<script language="JavaScript">
window.onbeforeunload = confirmWinClose;
function confirmWinClose() {return "close?";}
</script>
Try this it will work. You were assigning the method to onload that need to execute when it event occur so its need to be like object. refer link for better explanation - var functionName = function() {} vs function functionName() {}
window.onbeforeunload = confirmWinClose;
function confirmWinClose () {
var confirmClose = confirm('Close?');
return confirmClose;
};

how to call a logout code after runs onbeforeunload

have something like this in my main page:
<script language="javascript">
window.onbeforeunload = function(evt) {
var message = 'Are you sure you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
</script>
What I want to do is call my logout piece of code in order to free all resources, but only if the user press leave page bottom, is that possible... know witch option did the user choice?
Thank you all, best regards
use the onunload event:
window.onunload = function () {
// your logout code
};
var myclose = false;
$(window).on('beforeunload', function(){
var messege = 'You have closed the browser. Do you want to logout from your application?';
setTimeout('myclose=false',10);
myclose=true;
return myclose;
});
$(window).on('onunload', function(){
if (myclose==true)
{
//the url of your logout page which invalidate session on logout
alert('hello');
location.replace('logout.php') ;
}
});
var myclose = false;
$(window).on('beforeunload', function(){
var messege = 'You have closed the browser. Do you want to logout from your application?';
setTimeout('myclose=false',10);
myclose=true;
return myclose;
});
$(window).on('onunload', function(){
if (myclose==true)
{
//the url of your logout page which invalidate session on logout
alert('hello');
location.replace('logout.php') ;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Categories