gpt4 book ai didi

javascript - 为什么在 Chrome 和 FF 中调用函数 onclick 并不总是有效,但在 IE 中有效?

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:50 25 4
gpt4 key购买 nike

我有以下代码:

<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 功能吗?我还有其他选择吗?

我正试图帮助一位客户弄清楚为什么他们的一个跟踪标签在这些浏览器中点击时没有一直触发。

谢谢,

最佳答案

正如 Musa 所指出的,您遇到了某种依赖于浏览器的竞争条件。

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

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

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

然后在 myFunction() 下方添加您的 javascript,例如:

 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() 究竟应该完成什么?

关于javascript - 为什么在 Chrome 和 FF 中调用函数 onclick 并不总是有效,但在 IE 中有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com