Running script when leaving page

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Running script when leaving page

Post by simonmlewis »

Code: Select all

echo '<a href="javascript:window.close();">Close Window</a>';
Hi
I want to run a PHP script when a person leaves a window - not closing, just leaving it.
It's for a chat service we are looking at building, and need to close sessions and cookies, if they decide to either close the window, or click on a link on the page and go elsewhere.

The link above closes the window, that's fine - but need to ask the browser "are you being closed" and "are you being taken away from this page".

Is there such a script? I've been on web sites where a popup appears if you go to another site. So it must be possible.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Running script when leaving page

Post by AbraCadaver »

Not tested:

Code: Select all

window.onunload = my_function;

function my_function() {
   /* window.close(); or other stuff */
}
Might have to put the window.onunload() in the body tag, not sure. Or possibly:

Code: Select all

window.onunload = window.close();
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

Sorry to sound a bit dim here, but I need to run a script when that Function is performed, but I don't know how to put a PHP function into it?

Code: Select all

<script type="text/javascript">
function windowclose() {
// DOES PHP FUNCTION GO IN HERE?
window.onunload = window.close();
}
</script>
</head>
<?php

echo "<body //WHAT DO I PUT HERE?">";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

And the answer seems to be (after a bit more digging)

Code: Select all

<script>
window.onbeforeunload = function () {
    document.createElement('img').src = 'end.php';
}
</script>
When the window is closed by the 'x' in the browser (not a hand-made button), end.php is run and it performs all scripts needed.

So simple!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Running script when leaving page

Post by McInfo »

This is one of the most fundamental ideas in PHP programming. It is important for you to understand. For PHP to react to something the user does, an HTTP request must be made to the server.

JavaScript can trigger an HTTP request in a number of ways: [1] using the XMLHttpRequest object (AJAX), [2] inserting a tag which requires an external resource (<img>, <link>, <script>, <iframe>, etc.), [3] changing the src or href of such a tag, [4] (I could have overlooked something). Anyway, the point is that there must be a separate request.

I see you have discovered this already, but I will post my example because I had it prepared already and I think the comments will be helpful.

This example logs the current server time into a file (on the server, of course) when you close the browser window.

page.html - The first request is for this HTML document. When the page loads, your browser makes a second request, for close.js.

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <title>Close</title>
        <script type="text/javascript" src="close.js"></script>
    </head>
    <body>
        <p>Close me.</p>
    </body>
</html>
close.js - When you close the page, JavaScript inserts an image tag. When the image is inserted, your browser makes its third request, for ping.php.

Code: Select all

window.onunload = function () {
	var img = document.createElement('img');
	img.src = 'ping.php';
	document.body.appendChild(img);
}
ping.php

Code: Select all

<?php
file_put_contents('ping.txt', date('Y-m-d H:i:s'));
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Running script when leaving page

Post by AbraCadaver »

And here was my stab at the xmlhttp request (not tested):

Code: Select all

window.onunload = function ()
{
   xhttp = new XMLHttpRequest();
   xhttp.open("GET", "http://example.com/end.php");
   xhttp.send();
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

None of these, or mine are going to work. I thought mine would, and does. But it has one major flaw.

It does it even when the page changes. I want it to work only when the browser itself is closed.
When someone his submit to send more info, it posts it to itself, but in effect, it is closed and reopening that page, which causes the script to run and close the session.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

Can PHP identify if the page has been 'changed' or if the Browser itself has been closed?

We've had issues where a user of the site has gone from one page to another within the site, and it logs them out as thes script has executed.

I want to have it run ONLY when the browser is CLOSED.

Is this possible - while leaving it running if the page is changed?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Running script when leaving page

Post by califdon »

I don't think so. As was emphasized above, once a web server has sent a page to a browser, the server can only respond to a subsequent request. So PHP cannot "identify if the page has been 'changed' or if the browser itself has been closed". That would require the browser to send a new request, which might be initiated by Javascript, as discussed earlier. So the question to ask is whether Javascript (not PHP) can detect when the browser is closed, not redirected to another page. As far as I know, it cannot.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

Well yes, but I am in a Javascript forum so thought someone expert in JS might be able to respond with a solution.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Running script when leaving page

Post by califdon »

simonmlewis wrote:Well yes, but I am in a Javascript forum so thought someone expert in JS might be able to respond with a solution.
That's fine, this is the appropriate forum, but your questions have been:
I don't know how to put a PHP function into it
Can PHP identify if the page has been 'changed' or if the Browser itself has been closed?
I am just trying to insure that you understand that PHP has no way of knowing WHAT your browser is doing, other than receiving a request, so your entire issue is with Javascript, contrary to the questions that you asked. If you have that concept firmly in mind, you can disregard my comments.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Running script when leaving page

Post by simonmlewis »

Yes. I did just wonder if PHP is a method of doing something clientside.
The script I used above did work, but now doesn't. But it still closed the cookie session (ran the script in a file it ran on "unload"), even when just leaving that particular page and stayed in the site.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply