我正在尝试使用如下所示的 Facebook 转换代码,其中包括
script 和 noscripts 标签:
<html>
<head>
<script type="text/javascript">
// Wait for the page to load first
window.onload = function() {
//Get a reference to the link on the page
// with an id of "mylink"
var a = document.getElementById("mylink");
//Set code to run when the link is clicked
// by assigning a function to "onclick"
a.onclick = function() {
// Code to be executed when the link is clicked.
var _fbq = window._fbq || (window._fbq = []);
...
}
}
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=6030151253043&cd[value]=0.00&cd[currency]=USD&noscript=1" /></noscript>
</head>
<body>
<a id="mylink" href="http://example.com">Link</a>
</body>
</html>
通常我可以将此代码放在我网站的感谢页面中。但现在我有一种情况,我想跟踪是否有人单击外部链接,该链接会导致某些不受我控制的外部网页。我的页面不应在加载时执行此转换代码,而应仅在单击链接时执行。
例如,我的页面有一个链接到http://example.com http://example.com,所以当用户点击这个链接时,可以执行这段转换代码,然后转到http://example.com http://example.com?
问题是,当浏览器不支持JavaScript时,即使没有点击链接,noscript标签内的代码也会执行。
有什么办法可以解决这个问题吗?
您是否尝试过使用JavaScript onclick http://www.w3schools.com/jsref/event_onclick.asp事件?例如:
var el; // This is your element pointing to the link you want to handle
el.onclick = function() {
// Execute your code here, this event is fired before the default behaviour is executed which is to lead the user to the URL
};
如果您的 el 的 ID 为 mylink,那么您只需将 el 指定为
var el = document.getElementById('mylink');
如果您使用像 jQuery 这样的 JS 库,那么这段代码会变得更容易处理,但目前我假设您没有使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)