gpt4 book ai didi

javascript在后台加载url

转载 作者:行者123 更新时间:2023-11-30 10:53:55 27 4
gpt4 key购买 nike

我记得几年前,我使用了来自某处的 hack 在后台更新链接点击。我记得我的链接上有一个 onclick 事件,该事件触发了一个 javascript 函数,该函数试图加载图像或其他东西(这是 hack),但您输入的不是图像(或其他任何图像),而是像'mysite.com/updatehits.php?id=3'

希望这是有道理的:S

最佳答案

假设你有这个链接:

<a id="link" href="foo.html">Click for foo</a>

您希望用户访问该链接,但通过 ajax 透明地调用“点击计数器”。可以这样做:

$("#link").click(function(e) {

// prevent the link from getting visited, for the time being
e.preventDefault();

//update the counter
$.post("counter.php" {incrementCounter: this.href}, function(resp) {
if(resp == "success") {
alert("updated");
} else {
alert("failed");
}
// updated. Now visit this link as normal
window.location.href = this.href;
});
});

不过,我认为计算观看次数最好在服务器端完成。此外,这很可能会在访问链接时给用户带来恼人的可察觉延迟。

关于javascript在后台加载url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3414400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com