IndianaGuy Posted December 21, 2016 Share Posted December 21, 2016 It was hard deciding where I should put this under the PHP. HTML, OR JAVASCRIPT CODE. Is there a way to trigger a function when the user closes the window? It's a JavaScript function that calls for sending data to a PHP file using Ajax. Thank you Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2016 Share Posted December 21, 2016 Javascript is the only thing that directly responds to user interaction. You can run a function when the page is unloaded (which will also happen if they just go to a different page), or the window object does have a close event also. Link to comment Share on other sites More sharing options...
IndianaGuy Posted December 21, 2016 Author Share Posted December 21, 2016 So Javascript will have to open a .txt file and document something instead of sending the information to a PHP file? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2016 Share Posted December 21, 2016 No, you can have Javascript send data to PHP (although since the window is closing you'll want to test to make sure that works correctly), but PHP itself can't interact with the browser directly. Only Javascript can, so you need to start with Javascript. Javascript can't write to a text file though, so you need to have it send data to PHP to save on the server. Link to comment Share on other sites More sharing options...
IndianaGuy Posted December 21, 2016 Author Share Posted December 21, 2016 I am pretty sure I am on the right track, but not there yet. How is this so far? <body onbeforeunload="myLeaving()"> <script> function myLeaving(){ var Expo = document.getElementById("exposure"); // This is a video var ct = ConvertTime(Expo.currentTime); var Action = " He left the domain"; RecordAction(Member,Prospect,FileName,Action,ct); } Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2016 Share Posted December 21, 2016 That's fine in general, if you have those functions written to get and send the data. Link to comment Share on other sites More sharing options...
IndianaGuy Posted December 22, 2016 Author Share Posted December 22, 2016 It's recording the information when the page is left. It doesnot record when the user "x" the tab on the browser window. Any ideas how to make it record at that time as well? Thank you Link to comment Share on other sites More sharing options...
IndianaGuy Posted December 22, 2016 Author Share Posted December 22, 2016 I figured it out. window.onbeforeunload = function() { //my stuff here }; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now