可能的竞争条件? [英] Possible Race Condition?

查看:79
本文介绍了可能的竞争条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<HTML>

<HEAD>
  <SCRIPT>
  function myFunction(atlasTrackingURL)
  {
    var atlasURL = atlasTrackingURL;
    if (!atlasURL) return;

    //Build a cache busting mechanism
    var timestamp = new Date();
    var queryString = "?random=" + Math.ceil(Math.random() * 999999999999) + timestamp.getUTCHours() + timestamp.getUTCMinutes() + timestamp.getUTCSeconds();
    //Build the final URL
    atlasURL = atlasURL + queryString;

    if (!document.getElementsByTagName || !document.createElement || !document.appendChild)
      {return false;}
    else
      {     //Activate the JACTION call
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.onload = function()
        {
            document.getElementsByTagName("head")[0].appendChild(script);
        };
        script.src = atlasURL;
        return false;
      }
  }
  </SCRIPT>
</HEAD>

<BODY>

<a href="http://www.microsoft.com" onclick = "myFunction('http://view.atdmt.com/jaction/adoakb_PiggybackJSTest_1')">Test - Click Me</a>

</BODY>
</HTML>

它每次都可以在 Internet Explorer 中使用,但很少在 Chrome 和 Firefox 中使用.为什么会这样?

It works in Internet Explorer every time, but rarely works in Chrome and Firefox. Why would this be?

有趣的是,如果我将 URL (microsoft.com) 替换为 #,它确实可以正确执行.我想知道是否存在某种竞争条件,其中 href 在 onclick 函数加载之前进行评估.添加 # 会阻止 href 执行,因此 onclick 函数有时间触发.有什么办法可以解决这个问题吗?

What's interesting is that if I replace the URL (microsoft.com) with a #, it does execute properly. I'm wondering if there is a race condition of some kind where the href evaluates before the onclick function loads. Adding a # prevents the href from executing, and so the onclick function is given time to fire off. Is there any way to fix this?

我试图帮助客户弄清楚为什么他们的一个跟踪标签在这些浏览器中点击时没有一直触发.应该有一个在点击时执行的 javascript 文件(view.atdmt.com URL),但它只在 IE 中有效,在 FF 和 Chrome 中不能触发.

I am trying to help a client figure out why one of their tracking tags are not firing off all the time on click in these browsers. There is supposed to be a javascript file that gets executed on click (the view.atdmt.com URL), but it only works in IE and doesn't fire off in FF and Chrome.

我几乎肯定实际代码没有任何问题 - 因此我相信这些浏览器无法足够快地执行 onclick 并且首先执行登录页面.一旦到达登陆页面,就不能再调用 onclick 函数了....

I am almost positive there is nothing wrong with the actual code - so hence I believe these browsers can't execute the onclick fast enough and the landing page executes first. Once the landing page is reached, the onclick function can no longer be called....

谢谢,

推荐答案

当您从函数返回 true 时,允许链接的默认操作,浏览器继续导航到页面.

As you are returning true from the function, the default action of the link is allowed and the browser goes on to navigating to the page.

更改文档和排队请求时发生的情况可能会有所不同,因为浏览器的实现需要做一些合理的事情,并且请求脚本和不执行脚本都在合理范围内.

Exactly what happens with changes to the document and queued requests may differ, as it's up to the implementation of the browser to do something that is reasonable, and both requesting the script and not doing it are within what's reasonable.

当您离开页面时,您不能期望浏览器在当前页面中实际执行任何操作.有些浏览器可能会继续运行,直到新页面开始到达时才发生任何事情,有些浏览器可能会暂停某些操作,因为它们会请求响应到达时不应该再存在的页面的数据.

As you are navigating away from the page, you can't expect the browser to actually do anything more in the current page. Some browsers may go on as nothing happens until the new page starts to arrive, some may suspend some actions as they would request data for a page that isn't supposed to be there any more when the response arrives.

要使其在任何浏览器中都能正常工作,您应该从函数中返回 false 以防止链接被激活,并在导航到新页面之前等待脚本加载.

To make it work in any browser you should return false from the function to keep the link from being activated, and wait for the script to load before you navigate to the new page.

在函数中添加一个参数来处理 URL:

Add a paramter to the function to handle the URL:

function myFunction(url, atlasTrackingURL)

使用 onload 事件在脚本加载后导航到 URL:

Use the onload event to navigate to the URL when the script has loaded:

var script = document.createElement("script");
script.type = "text/javascript";
script.onload = function() {
  window.location.href = url;
};
script.src = atlasURL;
document.getElementsByTagName("head")[0].appendChild(script);
return false;

在事件处理程序中返回结果以阻止链接被激活:

Return the result in the event handler to stop the link from being activated:

<a href="http://www.microsoft.com" onclick = "return myFunction(this.href, 'http://view.atdmt.com/jaction/adoakb_PiggybackJSTest_1')">Test - Click Me</a>

这篇关于可能的竞争条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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