HTML锚点标签在Ajax请求后重定向链接

5

我正在尝试编写一个方法来捕获用户在网站菜单上的点击,但我希望这个过程对用户来说是静默的,主要是当用户悬停在锚点标记上时,它会正常地表现出来(显示它包含的链接)。我已经尝试了各种方式来做到这一点,现在我几乎已经成功了,我的代码如下:

<div class="selectNode" id="selectNode_1" style="color: rgb(229, 229, 229); background-color: rgb(47, 47, 47); ">
  <a href="http://www.google.com" class="selectAnchor" style="color: rgb(229, 229, 229); ">
    Google
  </a>
  <img class="childImage" src="icon.png">
</div>

在 jQuery 中,我有:

$(".selectAnchor, .menuAnchor").click(function(event){
    event.stopPropagation();
    console.log(event.target.nodeName);
    //event.preventDefault();

    $.ajax({
      type: 'POST',
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        return true;
      }
    });
});

链接可以重定向到选定的页面,但是ajax代码永远不会完成,因此无法注册用户的点击。
我尝试使用event.preventDefault,但是没有办法恢复取消的事件。
部分问题来自额外的功能,例如,大多数用户右键单击锚标记以在新标签页中打开(或使用ctrl +单击或中间单击),使用类似于
<a href="javascript:saveClick(o)">Google</a>

由于安全原因,不允许在该网站上进行此操作。

有什么建议可以解决这个问题吗?

3个回答

14

使用 event.preventDefault,然后在成功后使用 location.href = self.href

$(".selectAnchor, .menuAnchor").click(function(event){
    event.preventDefault();
    console.log(event.target.nodeName);
    var self = this;

    $.ajax({
      type: 'POST',
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        location.href = self.href;
      }
    });
});

或者利用上下文属性来去除 var self = this

$(".selectAnchor, .menuAnchor").click(function(event){
    event.preventDefault();
    console.log(event.target.nodeName);

    $.ajax({
      type: 'POST',
      context: this,
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        location.href = this.href;
      }
    });
});

我尝试了类似的方法,对于左键点击是有效的,但是对于同时按下控制键+左键点击或者中键点击,则会在同一个窗口打开链接。而且一些链接还包含target="_blank"属性,所以这种方法对它们也不适用。 - desto
如果你想保留那个功能,你没有太多的选择。如果你不阻止事件发生,你的ajax请求将无法完成。如果你阻止事件,那么其他功能就无法被保留。 - Kevin B
你是否考虑过创建一个中间页面,在跟踪退出后进行重定向? - Kevin B
您可以在单击时更改href以指向您的重定向页面,然后您的重定向页面记录该操作并将用户重定向。 - Kevin B
似乎类似于使用 href="javascript:method()" 方法,我尝试了一下,但由于鼠标悬停文本不会成为最终链接,因此它要求被删除。除非有一种方法可以显示另一个文本。 - desto
谢谢您的建议!我尝试了一下,现在它几乎可以运行了,现在我只需要找到一种处理右键单击+在新标签页中打开的方法(如果可能的话),但现在这样就可以了! - desto

2
最新版本的Chrome浏览器无法完成ajax请求。由于浏览器页面从用户处获得了新的URL请求,因此浏览器会立即终止线程。
在网络选项卡中,有98%的情况下你可以看到ajax请求被取消了。
所以解决方法是使用sendBeacon API。这将异步执行POST请求。谷歌分析使用此代码来执行事件跟踪(例如点击)。 https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
 navigator.sendBeacon("{{url}}", param);

0

试试这个

<div class="selectNode" id="selectNode_1" style="color: rgb(229, 229, 229); background-color: rgb(47, 47, 47); ">
  <a href="#" class="selectAnchor" style="color: rgb(229, 229, 229); ">
    Google
  </a>
  <img class="childImage" src="icon.png">
</div>

然后在 jQuery 中是这样的:

$(".selectAnchor, .menuAnchor").click(function(event){
    event.preventDefault();
    console.log(event.target.nodeName);
    //event.preventDefault();

    $.ajax({
      type: 'POST',
      url: 'http://localsite/menuRedirect.php',
      data: {id:0, module:'Module',source:'Source'},
      complete: function(data){
        console.log("DONE");
        window.location = "http://www.google.com"
      }
    });
});

我尝试过类似的事情,对于左键单击的情况可以工作,但是控制键+左键单击或中键单击会在同一窗口中打开链接。一些链接还包含target="_blank",因此这种方法也无法在它们上面工作。 - desto
@desto - 已编辑,这一定会起作用,唯一的问题是你将看不到鼠标悬停时的链接。 - Scott Selby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接