My goal is to execute a GET
request in JavaScript when a download link is clicked. Upon clicking the link, a GET
request should be sent to a PHP script that tracks the number of times the link is clicked. Although I'm relatively new to JavaScript, I have some experience with it. I have confirmed that my getUrlVars()
function and the function for when a link is clicked are functioning properly, so the issue seems to be with my GET request implementation.
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
vars[key] = value;
});
return vars;
}
// Wait for page to load
window.onload = function() {
var a = document.getElementById("downloadLink");
var app = getUrlVars()["app"];
a.onclick = function() {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "download_count.php?app=" + app, true);
xmlhttp.send();
return true;
}
}