简体   繁体   English

Opera 中的 Document.body

[英]Document.body in Opera

I've got a problem with retrieving link to the body tag.我在检索到 body 标签的链接时遇到了问题。 I've tried:我试过了:

  1. document.body , unfortunately it is null document.body ,不幸的是它是 null
  2. search body by tagName while enumerating childs of document, it's found only the head tag:(在枚举文档的子项时按 tagName 搜索正文,它只找到了头标签:(
  3. document.getElementsByTagName , return undefined document.getElementsByTagName ,返回未定义

I'm trying to get link to the body tag in onload event handler.我正在尝试在 onload 事件处理程序中获取指向 body 标记的链接。 This is a HTML code of the page:这是页面的 HTML 代码:

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

Here the source code of adv.js :这里是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();

}) ();

Source code of adLoader.js : 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!!');

}) ();

Browser is Opera 11.10 OS Ubuntu Linux.浏览器是 Opera 11.10 OS Ubuntu Linux。 Is there way to get this link?有没有办法得到这个链接? My aim is to add div with position:fixed to the body tag.我的目标是添加带有position:fixed到 body 标签的 div。

myFunction() doesn't return a function, so you are assigning undefined to window.onload . myFunction()不返回 function,因此您将undefined分配给window.onload You probably mean window.onload = myFunction;您可能的意思是window.onload = myFunction;

Anyway, as written at the moment, the code runs immediately and the body element hasn't been reached.无论如何,正如目前所写的那样,代码会立即运行并且还没有到达 body 元素。

if the js code in head tag and the query the body tag before onload event, null should be got while browser just reading the head.如果head标签中的js代码和onload事件之前查询body标签,浏览器读取head时应该得到null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM