Próbuję przetestować, czy element DOM istnieje, a jeśli istnieje, usuń go, a jeśli nie istnieje, utwórz go.
var duskdawnkey = localStorage["duskdawnkey"];
var iframe = document.createElement("iframe");
var whereto = document.getElementById("debug");
var frameid = document.getElementById("injected_frame");
iframe.setAttribute("id", "injected_frame");
iframe.setAttribute("src", 'http://google.com');
iframe.setAttribute("width", "100%");
iframe.setAttribute("height", "400");
if (frameid) // check and see if iframe is already on page
{ //yes? Remove iframe
iframe.removeChild(frameid.childNodes[0]);
} else // no? Inject iframe
{
whereto.appendChild(iframe);
// add the newly created element and it's content into the DOM
my_div = document.getElementById("debug");
document.body.insertBefore(iframe, my_div);
}
Sprawdzanie, czy istnieje, działa, tworzenie elementu działa, ale usuwanie elementu nie. Zasadniczo wszystko, co robi ten kod, to wstawienie ramki iframe na stronę internetową poprzez kliknięcie przycisku. Chciałbym, aby iframe już tam było, aby ją usunąć. Ale z jakiegoś powodu zawodzę.