Gmail中的getElementById( 'canvas_frame')保持在谷

2019-10-17 12:24发布

我试着写JavaScript代码的Gmail的扩展名,当我试图让画布框我“的getElementById”保留返回null值。

下面是我使用的代码:

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

我收到以下错误:

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

我该如何解决这个问题?

Answer 1:

由于与ID的iframe canvas_frame并不需要在Gmail中。

要得到有关文件的引用,你可以先尝试去参考iframe#canvas_frame ,如果失败,检查当前情况下是正确的:

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') keeps returning null in Google Chrome