歌剧中的纪录片

Document.body in Opera

本文关键字:纪录片      更新时间:2023-09-26

检索body标记的链接时遇到问题。我试过:

  1. document.body,不幸的是它为空
  2. 在枚举文档的子级时,按tagName搜索body,只找到head标记:(
  3. document.getElementsByTagName,返回未定义

我正在尝试获取到onload事件处理程序中body标记的链接。这是一个HTML代码的页面:

<html>
    <head>
        <title>Some page</title>
        <script src="/adv.js" type="text/javascript"></script>
    </head>
    <body>
        This is text
    </body>
</html>

这里是adv.js:的源代码

(function () {
    var myRandom = function (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    };
    var myFunction = function () {
        var newScriptAddr = '/adLoader.js?r=' + myRandom(1,1000000);
        var fileref = document.createElement('script');
        if (typeof fileref != "undefined")
        {
           fileref.setAttribute("type", "text/javascript");
           fileref.setAttribute("src", newScriptAddr);
           document.getElementsByTagName("head")[0].appendChild(fileref);
        }
    };
    if (window.onload)
    {
        var currOnLoad = window.onload;
        window.onload = function () {
            currOnLoad();
            myFunction();
        };
    }
    else
        window.onload = myFunction();
}) ();

adLoader.js:源代码

(function () {
    var mainCnt = document.createElement('div');
    mainCnt.appendChild(document.createTextNode('The text'));
    var _body = document.body;
    if (!_body)
    {
        var htmlTag = document.documentElement;
        for(var i = 0; i < htmlTag.childNodes.length; i++)
        {
            if (htmlTag.childNodes[i].nodeName.toLowerCase() == 'body')
            {
                _body = htmlTag.childNodes[i];
                break;
            }
        }
    }
    if (!_body)
        _body = document.getElementsByTagName('BODY') [0];
    if (!_body)
        _body = document.getElementsByTagName('body') [0];
    if (_body)
        _body.appendChild(mainCnt);
    else
        alert('WTF!!');
}) ();

浏览器是Opera 11.10操作系统Ubuntu Linux。有办法得到这个链接吗?我的目标是将带有position:fixed的div添加到body标记中。

myFunction()不返回函数,因此您将undefined分配给window.onload。你可能是指window.onload = myFunction;

无论如何,正如目前所写的那样,代码会立即运行,并且还没有到达body元素。

如果head标记中的js代码和onload事件之前的查询body标记,则在浏览器读取head时应该得到null。

相关文章: