简体   繁体   中英

Send GET variables without reloading the page with javascript

What I am trying to do is run a GET request in javascript when a download link is pressed. When the link is pressed it would send a GET request to a php script that basically counts how many times the link is pressed. Here is my code but I can't seem to get it to work right. I'm pretty new to javascript but still know my way around. I have tested and My getrulvars() function works and my function when a link is pressed works so I am sure that its my GET request that it not working

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;
    }
}

So I got it working what I had to do is return false then in my php script have it download the file. I guess when I was downloading the file it wouldn't allow the jQuery to send the GET value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM