0

I'm simply trying to remove an iframe from the DOM using a chrome extension I'm building.

This code isn't removing the 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?

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • Is the `id="tracking"` on the iframe element itself? Or on some element within the iframe? What errors do you see in the console? – jfriend00 Sep 19 '14 at 07:23
  • @jfriend00 the id is the iframe element. The error is `"Cannot read property 'parentNode' of null"` – Paul Dessert Sep 19 '14 at 07:34

1 Answers1

1

With the error: "Cannot read property 'parentNode' of null", there are several possibilities:

  1. There is no element in your document with the requested 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.

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.

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. If not, you can always use this plain javascript function for detecting when the document is loaded.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979