mootools 内联 Ajax 调用不起作用
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
onclick
- 非常 1995 年。而是将事件附加到元素并使用
event.stop()
,即:JS:
顺便说一句。
这在 HTML 中无效,ID 需要以字母开头。
Don't use
onclick
- very 1995.Instead attach an event to the element and use
event.stop()
, ie:JS:
Btw.
<div id="1">
this is not valid in HTML, an ID'd needs to start with a letter.好的。找到了问题之一的答案:
看来这是由于脚本不同步造成的。因此,它可能会在没有实际完全提交请求的情况下返回,然后浏览器会移动到另一个页面,从而中断执行。向脚本添加同步调用可以解决该问题:
onmousedown事件中提到的第二个问题,即如果浏览器导航回来,firefox不执行ajax调用仍然没有解决。不过,我将其保留,因为这不是提出的主要问题。
OK. Found an answer to one of the questions:
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:
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.