javascript not working in OPERA

北战南征 提交于 2019-12-12 01:59:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!