简体   繁体   English

发送GET变量而无需使用javascript重新加载页面

[英]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. 我想做的是,当按下下载链接时,在javascript中运行GET请求。 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. 当按下链接时,它将向php脚本发送一个GET请求,该请求基本上计算了按下链接的次数。 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. 我对javascript还是很陌生,但仍然知道我的方法。 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 我已经测试过,并且我的getrulvars()函数可以正常工作,按下链接时我的函数也可以正常工作,因此我确定它的GET请求不起作用

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. 所以我得到它的工作,我必须做的是返回false,然后在我的PHP脚本中下载文件。 I guess when I was downloading the file it wouldn't allow the jQuery to send the GET value. 我想当我下载文件时,它不允许jQuery发送GET值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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