jQuery ajax 功能在 Safari 中不起作用(Firefox、Chrome、IE 可以) [英] jQuery ajax function not working in Safari (Firefox, Chrome, IE okay)

查看:25
本文介绍了jQuery ajax 功能在 Safari 中不起作用(Firefox、Chrome、IE 可以)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 javascript 专家,但我有点困惑这在三个主要浏览器中是如何工作的,但不是 Safari ......这段代码有什么问题吗?基本上,我只是将它与给定 url 处的 php/mysql 回调结合使用来跟踪链接点击.

I'm no javascript wiz, but am a bit puzzled as to how this is working in three major browsers, but not Safari... is there something wrong with this code? Basically I'm just using this in conjunction with a php/mysql callback at the given url to track link clicks.

Drupal.behaviors.NodeDownloadCounter = function() {

    $('a.ndc-link').click(function() {
        $.post('http://www.pixeledmemories.com/node-download-counter/log/' + this.name);
        return true;
    });

};

在这里使用 Drupal 行为而不是

Using Drupal behaviors here instead of

$(document).ready(function() {

(正确的 Drupal 方法)但我已经尝试了两种方法,但没有任何区别.

(correct Drupal method) but I've tried it both ways and it doesn't make a difference.

我也试过删除return true",但没有效果.

I've also tried removing "return true", but with no effect.

好的,进一步的测试表明,让点击触发警报在 Safari 中确实有效:

Okay, further testing reveals that having the click trigger an alert DOES work in Safari:

$('a.ndc-link').click(function() {
    alert('testing (ignore)');
    $.post('http://www.pixeledmemories.com/node-download-counter/log/' + this.name);
    return true;
});

但仍然没有任何内容记录到 mysql.这是我的回调函数:

But still nothing being logged to mysql. Here is my callback function:

function node_download_counter_log($nid)
{
    global $user;
  $timestamp = time();
    $title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));

  db_query("INSERT INTO {node_download_counter} (nid, title, download_count, last_download, last_uid) VALUES (%d, '%s', %d, %d, %d) 
                    ON DUPLICATE KEY UPDATE download_count=download_count+1, last_download = %d, last_uid = %d", $nid, $title, 1, $timestamp, $user->uid, $timestamp, $user->uid);

  db_query("INSERT INTO {node_download_counter_log} (nid, title, uid, timestamp) VALUES (%d, '%s', %d, %d)", $nid, $title, $user->uid, $timestamp);

}

推荐答案

听起来问题是浏览器在数据发布完成之前正在更改页面.您可以尝试添加 return false 以查看它是否开始工作.如果是这样,您将需要在点击链接之前添加一个短暂的延迟.

Sounds like the problem is the browser is changing the page before the data post can be finished. You can try adding return false to see if it starts working then. If it does, you are going to need to add a short delay before following the link.

更新:由于它有效,请尝试在return true;"之前添加以下内容;

UPDATE: Since it works try adding the following before "return true;"

if(jQuery.browser.safari){
  setTimeout("window.location.href= '"+this.href+"'",500);
  return false;
}

这篇关于jQuery ajax 功能在 Safari 中不起作用(Firefox、Chrome、IE 可以)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆