Gmail getElementById('canvas_frame')在Google Chrome中始终返回null [英] Gmail getElementById('canvas_frame') keeps returning null in Google Chrome

查看:148
本文介绍了Gmail getElementById('canvas_frame')在Google Chrome中始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为Gmail扩展程序编写JavaScript代码,并且当我试图获取画布框架时,getElementById会一直返回 null 值。



以下是我使用的代码:

  var doc2 = document.getElementById('canvas_frame')。contentDocument; 

我收到以下错误:

< pre $ Uncaught TypeError:无法读取属性'contentDocument'为null

如何解决这个问题?

解决方案

因为iframe的ID canvas_frame

要获得对相关文档的引用,您可以先尝试获取对的引用, iframe#canvas_frame ,如果失败,检查当前上下文是否正确:

  var doc2 = document.getElementById('canvas_frame'); 
if(doc2){
doc2 = doc2.contentDocument;
} else if(document.getElementById('js_frame')){
//如果#canvas_frame不存在,但是#js_frame确实存在,那么Gmail会呈现
// main = top)frame
doc2 = document;
} // else not Gmail's document and doc2 === null


i'm trying to write JavaScript code for a Gmail extension, and when I'm trying to get the canvas frame I, "getElementById" keeps returning the null value.

Here is the code I'm using:

var doc2 = document.getElementById('canvas_frame').contentDocument;

I'm getting the following error:

"Uncaught TypeError: Cannot read property 'contentDocument' of null"

How can I solve this?

解决方案

Because the iframe with ID canvas_frame does not need to exist in Gmail.

To get a reference to the relevant document, you can first try to get a reference to iframe#canvas_frame, and if that fails, check if the current context is correct:

var doc2 = document.getElementById('canvas_frame');
if (doc2) {
    doc2 = doc2.contentDocument;
} else if (document.getElementById('js_frame')) {
    // If #canvas_frame does not exist, but #js_frame does, then Gmail renders
    // everything in the main (=top) frame
    doc2 = document;
} // else not Gmail's document, and doc2 === null

这篇关于Gmail getElementById('canvas_frame')在Google Chrome中始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆