0

I have a function that is intended to count the occurrences of each word on a web page (from the same domain) I've read numerous posts about getting the innerhtml of an iframe, however I think I'm misunderstanding as this isn't working, and is just giving me an empty string. TagID is the location in my HTML doc I want to add these items to.

function filereader(tagID) {
    var x = document.getElementById(tagID);
    var newFrame = document.createElement("iframe");
    newFrame.id = "pageinframe";
    newFrame.src = document.getElementById("pagetoread").value;
    x.appendChild(newFrame);
    var innerDoc = document.getElementById("pageinframe");
    var innerDocContent = innerDoc.contentDocument.body.innerHTML;
    console.dir(innerDocContent);
    var split = innerDocContent.split(" "), obj = {};
    var y = document.createElement("p");
    for (var x = 0; x < split.length; x++) {
        if(obj[split[x]] == undefined) {
            obj[split[x]] = 1;
        }
        else {
            obj[split[x]]++;
        }
    }
    console.dir(obj);
    for (var i= 0; i < obj.length; i++) {
        var toadd = document.createTextNode(obj[i]);
        y.appendChild(toadd);
    }
    var x = document.getElementById(tagID);
    x.appendChild(y);
}
Chaz
  • 195
  • 4
  • 17
  • You are creating the iframe dynamically and assign a source to it, and then you expect the content to be available immediately - which most likely it won’t be. You should use a load event handler for the iframe, and read the content from within there. – CBroe Feb 13 '17 at 12:08
  • 1
    You probably need to wait for the iframe to load. See this question: http://stackoverflow.com/questions/1463581/wait-for-iframe-to-load-in-javascript – Peter B Feb 13 '17 at 12:08

0 Answers0