I'm on a mission to understand ajax and I have my sights set on saving the timeSpentOnPage value from the TimeMe.js library into my MySql database. I've successfully integrated the javascript library into my jsp page, like so:
<script type="text/javascript" src="<c:url value="/resources/js/timeme.js" />"></script>
<script type="text/javascript">
TimeMe.initialize({
currentPageName: "listing", // current page
idleTimeoutInSeconds: 30 // seconds
});
</script>
There's an example provided on the github page for making an http request and utilizing the timeSpentOnPage variable:
window.onbeforeunload = function (event) {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","ENTER_URL_HERE", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
xmlhttp.send(timeSpentOnPage);
};
I'm seeking guidance on how I can incorporate the timeSpentOnPage variable into my database using ajax. Any insights or advice would be greatly appreciated.
Many thanks in advance!