mootools 内联 Ajax 调用不起作用

发布于 2024-12-17 03:10:40 字数 821 浏览 2 评论 0原文

我正在尝试使用 mootools ajax 请求来记录传出链接的点击次数。到目前为止,这就是我正在做的事情。

每个链接如下所示:

<div id="1">    
<a href="http://stackoverflow.com/" onclick="clickRecord(1)">StackOverflow</a>
</div>

javascript 函数 clickRecord(id) 定义如下:

function clickRecord(id){
    var u = "record.php";   
    var req = new Request({
      method: 'post',
      url: u,
      data:{'id':id},
      onComplete:function(response){
      }
    }).send();

}

我遇到的问题是这样的。如果我添加一个 return false;对于 onclick="" 声明,一切正常,当然问题是单击不会将用户带到预期的页面。如果我没有则返回 false;那么 ajax 调用似乎永远不会执行。

我认为 onclick 事件应该首先执行,然后只执行默认操作。难道不是这样吗?

如果您使用 onmousedown 事件,则会出现更奇怪的情况。好像在 Firefox 上,如果你使用 onmousedown 事件,一旦你进入新页面,你不能简单地导航回旧页面,你必须刷新旧页面。否则调用不会被执行。在 IE 上不会发生这种情况。

I am trying to use mootools ajax requests to record clicks on outgoing links. So far here is what I'm doing.

Each link looks like follows:

<div id="1">    
<a href="http://stackoverflow.com/" onclick="clickRecord(1)">StackOverflow</a>
</div>

The javascript function clickRecord(id) is defined as follows:

function clickRecord(id){
    var u = "record.php";   
    var req = new Request({
      method: 'post',
      url: u,
      data:{'id':id},
      onComplete:function(response){
      }
    }).send();

}

The problem I have is this. If I add a return false; to the onclick="" declaration, everything works fine, of course the problem there is that click does not take the user to the intended page. If I do not have the return false; then it seems like the ajax call is never executed.

I thought the onclick event should execute first and then only the default action should execute. Is this not the case?

There is an even stranger scenario if you use onmousedown event instead. It seems like on Firefox, if you use the onmousedown event, once you go to the new page, you cant simply navigate back to the old page, you have to refresh the old page. Else the call is not executed. This does not happen on IE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-12-24 03:10:40

不要使用 onclick - 非常 1995 年。

而是将事件附加到元素并使用 event.stop(),即:

<a href="http://stackoverflow.com/" data-id="1">StackOverflow</a>

JS:

document.getElements('a').addEvents({
    click: function(event) {
        event.stop();
        var u = 'record.php';
        var req = new Request({
            method: 'post',
            url: u,
            data: {
                'id': this.get('data-id');
            },
            onComplete: function(response) {}
        }).send();
    }
});

顺便说一句。

这在 HTML 中无效,ID 需要以字母开头。

Don't use onclick - very 1995.

Instead attach an event to the element and use event.stop(), ie:

<a href="http://stackoverflow.com/" data-id="1">StackOverflow</a>

JS:

document.getElements('a').addEvents({
    click: function(event) {
        event.stop();
        var u = 'record.php';
        var req = new Request({
            method: 'post',
            url: u,
            data: {
                'id': this.get('data-id');
            },
            onComplete: function(response) {}
        }).send();
    }
});

Btw. <div id="1"> this is not valid in HTML, an ID'd needs to start with a letter.

你又不是我 2024-12-24 03:10:40

好的。找到了问题之一的答案:

  1. 通过 onclick() 声明时,内联 mootools 请求未执行。
    看来这是由于脚本不同步造成的。因此,它可能会在没有实际完全提交请求的情况下返回,然后浏览器会移动到另一个页面,从而中断执行。向脚本添加同步调用可以解决该问题:
函数clickRecord(id){
    var u = "记录.php";   
    var req = 新请求({
      异步:假,
      方法:'发布',
      网址:你,
      数据:{'id':id},
      onComplete:函数(响应){
      }
    })。发送();
}

onmousedown事件中提到的第二个问题,即如果浏览器导航回来,firefox不执行ajax调用仍然没有解决。不过,我将其保留,因为这不是提出的主要问题。

OK. Found an answer to one of the questions:

  1. The inline mootools request did not execute when declared through the onclick().
    It seems this was caused by the script not being synchronous. So probably it returns without actually fully committing the request, and the browser then moves to another page breaking the execution. Adding a synchronous call to the script fixes the problem:
function clickRecord(id){
    var u = "record.php";   
    var req = new Request({
      async:false,
      method: 'post',
      url: u,
      data:{'id':id},
      onComplete:function(response){
      }
    }).send();
}

The second problem that was mentioned on the onmousedownevent, i.e. firefox not executing the ajax call if the browser navigates back is still unsolved. However I am leaving that to be as that wasn't the main question raised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文