Jump to content

onbeforeunload


bPHP

Recommended Posts

Hi, I'm using the following code to check if the user wants to navigate away from my page:

 

window.onbeforeunload = unloadMess;
function unloadMess () {
if (!desconectado) {
        	if (!enviando) {
        		mess = "Los cambios realizados no se guardarán"
        		return mess;
        	}
}
}
form.onsubmit = onSubmit;
function onSubmit () {
        enviando = true;
}

 

However, I do not know where to add something I want to do if he navigates away. I need to call a function that calls a php if the user leaves.

 

I'm trying the following:

 

window.onunload = limpiar;

limpiar() {

phpFunc('bye');

}

 

But it is not working... Any ideas?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/199160-onbeforeunload/
Share on other sites

You are trying to mix technologies.

 

onbeforeunload is client side and operates using Javscript.

 

PHP is executed on the server and there is no PHP that you can execute from this context. 

Also,

window.onbeforeunload = unloadMess();

You need to call the function with () in javascript (and PHP for that matter)

 

Also, with your code someone can click submit and then navigate away.  The only way to properly test is in the PHP handler.  (the code the form calls)

Link to comment
https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045306
Share on other sites

I know where is php executed. But at the same time I know how to call a php function from javascript, something I'm doing on my code. However, my problem is that I would like to clean the user data when he leaves (stuff in the database), and I don't know how to do it. Is there any way that if he presses OK in the navigate away from page message box I can call that php function?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045371
Share on other sites

I know where is php executed. But at the same time I know how to call a php function from javascript, something I'm doing on my code. However, my problem is that I would like to clean the user data when he leaves (stuff in the database), and I don't know how to do it. Is there any way that if he presses OK in the navigate away from page message box I can call that php function?

 

Thanks!

 

You need to use ajax to do this. Look up jquery and ajax functions ;)

Link to comment
https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045377
Share on other sites

If you use jquery ajax in the unload function, be sure to set asynchronous = false using the full low-level ajax();  ;)

 

Edit: I just found out yesterday it is sporadic without the async setting, took a while to debug. Here is how mine was implemented for a client:

 


$(window).unload(function(){

	$.ajax({
		async:false,
		type:"GET",
		url:"/index.php",
		data:'direct=modules/chat/chat.php&action=closechat&which='+$("#whichChat").val(),
		cache:false
	 });

});

Link to comment
https://forums.phpfreaks.com/topic/199160-onbeforeunload/#findComment-1045401
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.