hi, Can someone please help me with this: <script type="text/javascript"> window.onbeforeunload = function (evt) { if (typeof evt == 'undefined') { evt = window.event; } if (evt) { <a href="http://www.1stacademy.net/hello.php"> } return; } </script> I have inserted the code above into a html page so the javascript gets called when the user leaves my web page. what I want to be able to do is execute the file "hello.php" when someone leaves the page. Can anyone tell me what I would replace the line <a href="http://www.1stacademy.net/hello.php" with to do this? Thanks Derek
switch <a href="http://www.1stacademy.net/hello.php"> Code (markup): with window.location='http://www.1stacademy.net/hello.php'; Code (markup): this will redirect the user to hello.php OR window.open ("http://www.1stacademy.net/hello.php","mywindow"); Code (markup): However this method will be blocked by all pop-up blockers. Another way is to use Ajax => http://www.w3schools.com/ajax/default.asp Pick one..
Thanks for that. Actually I've had some rather strange results with it. I wanted to put my php scripts that aren’t actual pages into a 'php_scripts' directory to keep them separate. however window.location doesn’t seem to like it. First of all I found that the only variation that would work is: window.location="hello.php"; (worked most of the time???) as soon as I changed it to (along with moving the file): window.location="php_scripts/hello.php"; No other variation including the whole address would work. What I am actually trying to do is log the amount of time a webpage remains open for. So I thought if I logged the time in my db using php on the page open I could then use javascript to call another php file when the user leaves the page. the other php file would calculate the time difference and store it in the db. Any ideas as to why window.location="php_scripts/hello.php"; doesn’t work? Or how else I might be able to log the durations users stay on my pages? I wondered if it was the browser opening a new page at the time was a problem. Really odd!
Can be as simple as the folder "php_scripts" have the wrong rights or the reason it might not work for all can be as simple as the user have Javascript disabled.
I tried changing file permissions but it didn't make any difference. I don't think there is a problem with the java being ON/OFF. It seems to work OK untill I put the php file in the directory. I also looked at the AJAX link but I am too new to all this to understand that. Is there any other way to do it or are you able to show me it in AJAX?
<script type="text/javascript"> function start_ajax() { var temp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari temp=new XMLHttpRequest(); } else { // code for IE6, IE5 temp=new ActiveXObject("Microsoft.XMLHTTP"); } return temp; } function SendRequest(url) { // Create Ajax object var http = start_ajax(); // Set how and where it should connect http.open("GET",url,false); // Send the request http.send(); } </script> Code (markup): where the window.location was just call SendRequest("php_scripts/hello.php"); Code (markup): Note: not tested, and for you out there that think it´s looks like w3schools functions.. it´s because it is based on those (could not be arsed to write something from scratch at this hour of the day)
Thanks for taking the time to writing that. I’ve tested it and as annoying as it is, it actually works exactly the same as window.location! I’ve tried the absolute address, as well as just the dir in front but it makes no difference. Calling just hello.php by itself through works fine. Makes me want to go back to basics and assembler… Could it have anything to do with the fact that I am using it in html pages and not xhtml?
Just pure php - it's also being used with a mysql database, but I don't know whether the call to the file is failing or if it's the database failing in some way. (I wouldn't know what to look for with AJAX libraries.)