简体   繁体   English

在打开新窗口之前,如何确保单击功能中的所有代码都已执行?

[英]How do I make sure all of the code in an on click function gets executed before opening a new window?

I have a function that triggers on click that sends off one tracking event to Google, and one request to my server to create a record in the database. 我有一个触发点击的功能,该功能向Google发送一个跟踪事件,向我的服务器发送一个请求,以在数据库中创建一条记录。

Everything works fine for desktop users but for some mobile users Google will record click events that my database doesn't. 一切对于台式机用户都可以正常运行,但对于某些移动用户,Google会记录我的数据库不记录的点击事件。 After some research I think I've narrowed down the problem to the fact that opening a new window via a web view causes the previous window to stop executing JavaScript, so the request to my server never gets sent. 经过一些研究,我认为我已经将问题缩小为以下事实:通过Web视图打开新窗口会导致前一个窗口停止执行JavaScript,因此对我的服务器的请求永远不会发送。 I temporarily mitigated the problem by adding a small delay to the window opening but recently I've noticed that the built-in pop-up blockers in web browsers are blocking the window so I've had to remove the delay. 我通过在窗口打开时添加一个小的延迟来暂时缓解该问题,但是最近我注意到Web浏览器中的内置弹出窗口阻止程序正在阻止窗口,因此我不得不删除该延迟。

Html: HTML:

<a ng-click="captureClick(pageUrl)" target="_blank">

JS: JS:

$scope.captureClick = function(link){
  //Send Google Analytics click event
  ga('send', 'event', 'click');

  var linkObj = {
    //Create object with click details
  };
  Links.save(linkObj);

  $window.open(link);
};

Use Promises: 使用承诺:

new Promise(function(resolve, reject) {
  // your code to your server
  // after your server replied to the call
  resolve();
}).then(function(data) {
  // google track code
});

暂无
暂无

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

相关问题 在NodeJS中发送响应之前,如何确保执行“ for循环”中的函数? - How can I make sure that the function inside 'for loop' gets executed before I send the response in NodeJS? 在将值传递给res.render之前,如何确保函数完全执行? - How do I make sure a function is completely executed before its value is passed to res.render? 如何确保在运行所有其他代码之前创建了一个数组? - How do I make sure an array is made before all other code is ran? 如何确保在 java 脚本中的所有其他代码之前执行某些代码? - how to make sure some piece of code is executed before all other codes in java script? 如何确保在运行函数之前加载了所有资源? - How do I make sure all resources are loaded before running a function? 我想确保在Node.js服务器端脚本中的以下代码之前,我的上一个函数必须完全执行 - I want to make sure that my previous function must be executed completely before the following code in Nodejs server side scripting 如何确保代码在window.location.href之前运行 - How to make sure code runs before window.location.href 如何确保一个 function 调用的回调仅在第二个 function 调用回调后执行 - how to make sure callback of one function call, gets executed only after callback of the second function call 如何确保定义了该变量? - How do I make sure that this variable gets defined? AngularJs如何确保代码同步执行 - AngularJs how to make sure code executed synchronously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM