在使用 GA 跟踪 iframe 时,我向父页面添加如下脚本
<script>
setTimeout(function(){
ga(function(tracker) {
var clientId = tracker.get('clientId');
var frameWindow = document.getElementById('iframeId').contentWindow;
frameWindow.postMessage(clientId, 'https://parentpage.com');
});
}, 2000);
</script>
只要只有一个 iframe,就一切正常。
但是现在我有一个页面包含三个 iframe,它们具有自己的不同 ID,应该单独跟踪。
我应该如何处理某些 iframe 来分别跟踪每个 iframe?
最佳答案
您可以选择像这样的 iframe
let iframes=document.querySelectorAll("iframe");
iframes.forEach(elem=>{
console.log(elem.getAttribute("id"))// address your iframe separately here
})
<iframe id="one">
</iframe>
<iframe id="two">
</iframe>
关于javascript - 使用 javascript 获取多个 iframe 之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55967367/