Jump to content

AJAX script on page unload


henryhenry

Recommended Posts

HiI'm trying to track links clicked. I am using PHP and MySQL to store the referer in the database then I have an onmousedown event on all links which calls a php script through AJAX. This script updates the database to add the link that the user has clicked on for that page.When I test the setup, I find that if I trigger the onmousedown event without clicking the link (ie click then move the cursor away), the script runs nicely and records the correct information. When I click for real, the script doesn't seemt finish running so the information is lost.Does anyone know how I can delay the page from redirecting until the script is finished?I am aware of the option of have a holding page which runs the script but I think this is a bit intrusive and is of no use to the user so I am trying to avoid it.Perhaps another alternative is to run a php script in the background which continues while the user navigates away but I'm not sure how this can be done.Thanks in advance for your help!Henry

Link to comment
Share on other sites

You could also send the AJAX request synchronously rather than asynchronously by setting the third parameter to the open method to false.

xmlHttp.open("GET", "mypage.php?id=something", false);

Then, the scripting engine will have to wait until that request returns before continuing on with the execution of the hyperlink being clicked.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...