jQuery ajax功能无法在Safari中运行(Firefox,Chrome,IE还可以) [英] jQuery ajax function not working in Safari (Firefox, Chrome, IE okay)

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

问题描述

我不是javascript wiz,但是对于这在三个主流浏览器中是如何工作有点困惑,但不是Safari ...这段代码有什么问题吗?基本上我只是在给定网址上与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.

更新:
因为它有效,尝试在返回之前添加以下内容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天全站免登陆