开发者

JavaScript: Detection of a link click inside an iframe

How to dete开发者_Go百科ct user's click on the link inside the iframe using JavaScript?


Assuming you have an iframe with ID "myIframe", and the iframe comes from the same domain as the main document, the following will detect a click anywhere in the document. This will also work when the document is editable, which using the document's onclick property would not:

function iframeClickHandler() {
    alert("Iframe clicked");
}

var iframe = document.getElementById("myIframe");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

if (typeof iframeDoc.addEventListener != "undefined") {
    iframeDoc.addEventListener("click", iframeClickHandler, false);
} else if (typeof iframeDoc.attachEvent != "undefined") {
    iframeDoc.attachEvent ("onclick", iframeClickHandler);
}


You are able to check iframe load event

onLoad="alert(this.contentWindow.location);"

or on jquery:

$('iframe#yourId').load(function() {
  alert("the iframe has been loaded");
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜