繁体   English   中英

为什么调用函数onclick在Chrome和FF中不能一直工作,但在IE中工作?

[英]Why does calling a function onclick not work all the time in Chrome and FF, but works in IE?

我有以下代码:

<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.src = atlasURL;
                        document.getElementsByTagName("head")[0].appendChild(script);
                    return true;
                  }
      }
      </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中运行。 为什么会这样?

这些浏览器能否很好地处理onClick函数? 我还有其他选择吗?

我正在努力帮助客户弄清楚为什么他们的跟踪标签在这些浏览器中一直没有点击。

谢谢,

穆萨指出,你有一些依赖浏览器的竞争条件正在发生。

尝试最初隐藏链接,等待文档加载,然后添加onclick属性并使用javascript显示链接。

因此,例如,将链接HTML更改为:

<a id="microsoft-link" href="http://www.microsoft.com" style="display: none;">Test - Click Me</a>

并添加你的javascript,在myFunction()下面,类似于:

 document.addEventListener('DOMContentLoaded', function(){
   var link_elem = document.getElementById("microsoft-link");
   link_elem.onclick = function() {
     myFunction('http://view.atdmt.com/jaction/adoakb_PiggybackJSTest_1'); 
   };
   link_elem.style.display = 'inherit';
 });

jsfiddle - http://jsfiddle.net/m8VTy/3/

编辑:我意识到我可能会误解你要做的事情。 myFunction()应该完成什么?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM