简体   繁体   English

jQuery ajax函数在Safari中不起作用(Firefox,Chrome,IE可以)

[英]jQuery ajax function not working in Safari (Firefox, Chrome, IE okay)

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? 我不是javascript向导,但是对于这在三种主要浏览器中的工作方式却有点困惑,但对于Safari而言却不是...此代码有问题吗? Basically I'm just using this in conjunction with a php/mysql callback at the given url to track link clicks. 基本上,我只是将其与给定网址的php / mysql回调结合使用,以跟踪链接点击。

Drupal.behaviors.NodeDownloadCounter = function() {

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

};

Using Drupal behaviors here instead of 在这里使用Drupal行为,而不是

$(document).ready(function() {

(correct Drupal method) but I've tried it both ways and it doesn't make a difference. (正确的Drupal方法),但我已经尝试了两种方法,并且没有区别。

I've also tried removing "return true", but with no effect. 我也尝试过删除“返回true”,但是没有任何效果。


Okay, further testing reveals that having the click trigger an alert DOES work in Safari: 好的,进一步的测试表明,使点击触发警报确实可以在Safari中工作:

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

But still nothing being logged to mysql. 但是仍然没有任何东西被登录到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);

}

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. 您可以尝试添加return false来查看它是否开始工作。 If it does, you are going to need to add a short delay before following the link. 如果是这样,您将需要在链接之前添加短暂的延迟。

UPDATE: Since it works try adding the following before "return true;" 更新:由于它可以正常工作,请尝试在“ return true;”之前添加以下内容:

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

Okay, based on our conversation on comments above, try 好的,根据我们对上述评论的讨论,尝试

$('a.ndc-link').click(function() {
    var href = this.href;
    $.post('http://www.pixeledmemories.com/node-download-counter/log/' + this.name,
       function() {
           window.location.href = href;
       }
    );
    return false;
});

Firs,t you have to be careful not to attach your handler more than once to each 'a.ndc-link' , one way to do it is to tag the elements with a custom class. 首先,您必须小心不要将处理程序附加到每个'a.ndc-link' ,一种方法是使用自定义类标记元素。

Drupal.behaviors.NodeDownloadCounter = function() {
    $('a.ndc-link:not(.node-download-counter-processed)').addClass('node-download-counter-processed').click(function(event) {
        // ...
    });
};

One reason I see for this not to work is that, because it closes the page to open the link target, Safari will cancel the $.post request before it is actually sent to the server. 我之所以看不到它的一个原因是,因为它关闭了页面以打开链接目标,所以Safari会在实际将其发送到服务器之前取消$.post请求。 Returning false and calling event.preventDefault (event being the first argument of your event handler) should prevent this from happening but will also prevent the browser to actually load the link's target. 返回false并调用event.preventDefault (事件是事件处理程序的第一个参数)应会阻止这种情况的发生,但也将阻止浏览器实际加载链接的目标。 One way to solve this is to defer the page change until the POST request is complete. 解决此问题的一种方法是将页面更改推迟到POST请求完成之前。

Drupal.behaviors.NodeDownloadCounter = function() {
    $('a.ndc-link:not(.node-download-counter-processed)').addClass('node-download-counter-processed').click(function(event) {
        var link = this;
        event.preventDefault();
        $.post('http://www.pixeledmemories.com/node-download-counter/log/' + this.name, function() {
          window.location.href = link.href;
        });
        return false;
    });
};

But this will only works if there is no error in the POST request. 但这仅在POST请求中没有错误的情况下有效。

A better solution would be to hijack the server-side handler for the link target to add the click logging and then call the original handler. 更好的解决方案是劫持链接目标的服务器端处理程序,以添加单击日志记录,然后调用原始处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript Ajax请求无法在Firefox和Google Chrome中运行,但在Safari中可以 - JavaScript Ajax request not working in Firefox and Google Chrome, but it is okay in Safari jQuery适用于Chrome和Safari,但不能适用于Firefox或IE? - jQuery working in Chrome and Safari, but not Firefox or IE? jQuery ajax可在Chrome和Safari中使用,但不能在IE8和Firefox中使用 - Jquery ajax works in Chrome and Safari, but not in IE8 and Firefox Ajax 调用/javascript - 在 IE、Firefox 和 Safari 上运行良好,但在 Chrome 上运行不正常 - Ajax call/javascript - working fine on IE, Firefox and Safari but not on Chrome 可以在Chrome中正常工作的JavaScript jQuery代码,而不能在Firefox,Safari,IE中运行 - Simply javascript jQuery code working in Chrome, not Firefox, Safari, IE Javascript / jQuery无法在Firefox,Safari和IE中运行。 精通Opera和Chrome - Javascript/jQuery not working in Firefox, Safari and IE. Fine in Opera and Chrome jQuery .css()在IE 6,7,8和Firefox中不起作用,但在Chrome,Safari,Opera中起作用 - jQuery .css() not working in IE 6,7,8 and Firefox, but works in Chrome, Safari, Opera Wicket Ajax 在 Firefox 和 IE 中有效,但在 Chrome 和 Safari 中无效 - Wicket Ajax works in Firefox and IE but not Chrome and Safari Ajax提交可在Chrome / Safari中运行,但不能在Firefox / IE中运行 - Ajax submit works in Chrome/Safari but not Firefox/IE jQuery适用于Firefox,Safari,但不适用于Chrome - jQuery working in Firefox, Safari but not in Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM