Read data in HTML object tag

前端 未结 2 1403
春和景丽
春和景丽 2021-01-03 03:14

I have a text file stored on the server and an object in HTML like this:


         


        
2条回答
  •  我在风中等你
    2021-01-03 03:34

    The object tag has to make a separate request to the server and then load that content. Meanwhile, your JavaScript has already executed and "misses the bus."

    Run your code inside the onload event of the object.

    Then use .contentDocument.body.childNodes[0].innerHTML to view the text file.

    var object = document.getElementById("data");
    object.onload = function() {
        var data = object.contentDocument.body.childNodes[0].innerHTML;
        // use the data
    };
    

提交回复
热议问题