Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS判断评论框是否成功加载的方法 #20

Open
Meekdai opened this issue Aug 1, 2023 · 0 comments
Open

JS判断评论框是否成功加载的方法 #20

Meekdai opened this issue Aug 1, 2023 · 0 comments
Labels

Comments

@Meekdai
Copy link
Owner

Meekdai commented Aug 1, 2023

在给博客添加 utteranc 评论框的时候,发现添加按钮再加载评论框比较友好。但是发现点击后需要隐藏按钮元素,否则会比较奇怪,这个时候就需要通过JS来判断iframe是否成功加载了。

在网上找了一圈,发现 Iframes, onload, and document.domain 文中给出来比较详细的介绍和方法,但是用在utteranc的时候,出现了问题。

Uncaught TypeError: Cannot read properties of undefined (reading 'attachEvent')

定位发现,是由于iframe标签都还没有的时候,就去判断iframe.attachEvent是不行的。所以我曲线救国,通过定时器读取元素的高度来判断iframe标签是否完成了加载,正常情况下,height值都应该是存在的,所以代码如下:

int=self.setInterval("iFrameLoading()",200);

function iFrameLoading(){
    var utterances=document.getElementsByClassName('utterances');
    if(utterances.length==1){
        if(utterances[0].style.height!=""){
            int=window.clearInterval(int);
            console.log("utterances Load OK");
        }
    }
}

另外我还在官方提了个 Issue,不知道有没有更好的办法来判断。:rofl:

@Meekdai Meekdai added the 软件 label Aug 1, 2023
@Meekdai Meekdai changed the title JS判断utteranc评论框是否成功加载的方法 JS判断评论框是否成功加载的方法 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant