简体   繁体   English

从dom移除iframe

[英]removing an iframe from the dom

I'm simply trying to remove an iframe from the DOM using a chrome extension I'm building. 我只是想使用正在构建的Chrome扩展程序从DOM中删除iframe

This code isn't removing the iframe . 这段代码并没有删除iframe

var frame = document.getElementById( "tracking" ); 
frame.parentNode.removeChild( frame );

My manifest file is set up correctly. 我的清单文件已正确设置。 I'm able to access other elements on the page like this: 我可以像这样访问页面上的其他元素:

var bg = document.getElementById( "someId" ); 
bg.parentNode.removeChild( bg );

Why can't I remove the iframe ? 为什么无法删除iframe

With the error: "Cannot read property 'parentNode' of null", there are several possibilities: 出现错误:“无法读取null的'parentNode'属性”,有几种可能性:

  1. There is no element in your document with the requested id. 您的文档中没有包含所请求ID的元素。
  2. You are searching for the element BEFORE it has been loaded into the DOM by running your code too early in the document loading process. 您正在通过在文档加载过程中过早运行代码来搜索元素,然后再将其加载到DOM中。

If it's the second item, then your code either needs to be moved to the end of the body element (right before </body> ) or you need to use one of the event notifications that tell you when the page is loaded such as window.onload . 如果是第二项,那么您的代码要么需要移到body元素的末尾(在</body>之前),要么需要使用事件通知之一来告诉您页面何时加载,例如window.onload

If this is a Chrome extension, then I would assume there is some help in that extension framework to know when the document has finished loading. 如果这是一个Chrome扩展程序,那么我认为该扩展程序框架中有一些帮助可以帮助您了解文档何时完成加载。 If not, you can always use this plain javascript function for detecting when the document is loaded. 如果没有,您可以始终使用此简单的javascript函数检测何时加载文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM