cancel
Showing results for 
Search instead for 
Did you mean: 

A simple JavaScript / HTML issue. (Or so I thought)

Anonymous
Not applicable

A simple JavaScript / HTML issue. (Or so I thought)

I'm having an issue with this code from my JS file:

window.onbeforeunload = function() {
	console.log("Logging out") ;
	location.replace("http://secure.local.lan/logout.html") ;
	console.log("Logged out") ;
};

In that the page doesn't appear to be loaded. I need to force the user to load the logout.html page before they leave this secure vHost so Apache can free their login so they can log in again.

I can't help but think I am missing something here, as I see both the messages appear on the console, and the browser navigates to the link chosen (i.e. Home), but I know logout.html isn't called. Your thoughts would be appreciated.

If you need to know more, please ask.

4 REPLIES 4
dvorak
Moderator
Moderator
Posts: 29,497
Thanks: 6,627
Fixes: 1,483
Registered: ‎11-01-2008

Re: A simple JavaScript / HTML issue. (Or so I thought)

Mozilla suggest that it is better to use window.addEventListener() and the beforeunload event, instead of onbeforeunload.
Customer / Moderator
If it helped click the thumb
If it fixed it click 'This fixed my problem'
Anonymous
Not applicable

Re: A simple JavaScript / HTML issue. (Or so I thought)

I have already tried that @dvorak and it 'failed' in the same way so I tried this version of it.

Anonymous
Not applicable

Re: A simple JavaScript / HTML issue. (Or so I thought)

OK, after a bit of reading I have a fix using this and it's a more elegant solution all round (I think).

window.addEventListener('beforeunload', (event) => {
// Dump all of the session cookies document.cookie.split(";").forEach( (c) => { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
// Send a beacon notice to the logout page navigator.sendBeacon("http://secure.local.lan/logout.html", null) ; });

 

dvorak
Moderator
Moderator
Posts: 29,497
Thanks: 6,627
Fixes: 1,483
Registered: ‎11-01-2008

Re: A simple JavaScript / HTML issue. (Or so I thought)

Glad you sorted it
Customer / Moderator
If it helped click the thumb
If it fixed it click 'This fixed my problem'