问题
This javascript works in Firefox, IE, Chrome but Not in Opera. How to make it cross browser compatible? I need script to detect click on iframe.
<script>
var isOverIFrame = false;
function processMouseOut() {
isOverIFrame = false;
top.focus();
}
function processMouseOver() { isOverIFrame = true; }
function processIFrameClick() {
if(isOverIFrame) {
//was clicked
}
}
function init() {
var element = document.getElementsByTagName("iframe");
for (var i=0; i<element.length; i++) {
element[i].onmouseover = processMouseOver;
element[i].onmouseout = processMouseOut;
}
if (typeof window.attachEvent != 'undefined') {
top.attachEvent('onblur', processIFrameClick);
}
else if (typeof window.addEventListener != 'undefined') {
top.addEventListener('blur', processIFrameClick, false);
}
}
</script>
<iframe src="http://google.com"></iframe>
<script>init();</script>
回答1:
Try adding the following to your script
tags:
type="text/javascript"
回答2:
This is sort of an odd thing to do - your JavaScript should not be able to detect what the user does in an IFRAME from a different domain. The "blur" event is not a reliable indicator that the user clicked in the IFRAME (what if she just switched to another window?).
In short: what you're trying to do is against the architecture of web scripting, for good reasons that have to do with security and privacy, hence you can not do it reliably.
来源:https://stackoverflow.com/questions/11938744/javascript-not-working-in-opera