用document.activeElement解决iframe跨域问题

发布时间:2020-07-24 11:56:59 作者:maweiblue
来源:网络 阅读:2626

iframe里面一般都会使用别人的URL,没有很多交互,使用document.activeElement可以对iframe进行事件监听。
使用下面的类进行测试:


 var IframeOnClick = {
        resolution: 200,
        iframes: [],
        interval: null,
        Iframe: function() {
            this.element = arguments[0];
            this.cb = arguments[1];
            this.hasTracked = false;
        },
        track: function(element, cb) {
            this.iframes.push(new this.Iframe(element, cb));
            if (!this.interval) {
                var _this = this;
                this.interval = setInterval(function() { _this.checkClick(); }, this.resolution);
            }
        },
        checkClick: function() {
            if (document.activeElement) {
                var activeElement = document.activeElement;
                for (var i in this.iframes) {
                    if (activeElement === this.iframes[i].element) { // user is in this Iframe
                            this.iframes[i].cb.apply(window, []);
                    }
                }
            }
        }
    };

            var index = layer.open({
            type: 2,
            content: 'http://layim.layui.com',
            area: ['320px', '195px'],
            maxmin: true,
            success:function (layero,index) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                IframeOnClick.track(layero.find('iframe')[0], function() { console.log("testtttt") });
            }
        });
        layer.full(index);
推荐阅读:
  1. 怎么用Nginx解决前端跨域问题
  2. javascript怎么解决跨域问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript 跨域 问题

上一篇:VS创建cocos2d-x的win32工程移植android平台

下一篇:在CentOS上搭建基于Nodejs的Ghost博客

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》