Jump to content

AJAX script on page unload

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...