Home:ALL Converter>Close an injected iframe

Close an injected iframe

Ask Time:2012-09-22T01:19:42         Author:Mr T.

Json Formatter

I have a content script that runs from my chrome extension. this script injects an iframe to the body of the current page. i want to have the possibility to close the iframe from within the iframe. how do i do this? when i searched this issue on the web, almost each solution uses the window.parent.document property which for some reason is undefined in my case. any ideas?

EDIT - Code Sample:

in the HTML of the iframe:

<script type="text/javascript">
        function frameClose() {
            var windowFrames = window.parent.frames;
            for (var i = 0; i < windowFrames.length; i++) {
                var aFrame = windowFrames[i];
                if (aFrame.name == 'myFrame') {
                    alert('in frame');
                    // WHAT TO DO HERE?
                    // window.parent.document is undefined
                    // aFrame.parentNode.removeChild(aFrame); - THIS DOES NOT WORK ALSO
                    break;
                }
            }
        }
    </script>

this is how i inject the iframe:

Extension.js

chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.executeScript(null, {
        file : "/js/PushIFrame.js"
    }, function() {
        if (chrome.extension.lastError) {
        }
    });
});

and in PushIFrame.js i have:

chrome.extension.sendMessage({
    action: "pushFrame",
    source: pushIframe(document)
});

function pushIframe(document) {
    var existingFrame = document.getElementById('bmarkFrame');
    if (existingFrame == null) {
        var temp = document.createElement('iframe');
        temp.id = 'myFrame';
        temp.name = 'myFrame';
        temp.setAttribute('scrolling', 'no');
        temp.setAttribute('allowtransparency', 'true');
        temp.style.border = 'none';
        temp.style.height = '100%';
        temp.style.width = '100%';
        temp.style.position = 'fixed';
        temp.style.zIndex = 99999999;
        temp.style.top = 0;
        temp.style.left = 0;
        temp.style.display = 'block';
        temp.src = 'https://www.mysite.com/';

        document.body.appendChild(temp);
    }
    else {
        existingFrame.style.display = 'block';
    }
}

Author:Mr T.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12535182/close-an-injected-iframe
yy