Ошибка при получении документа. Помогите! - PullRequest
0 голосов
/ 04 сентября 2010

У меня есть следующий код JavaScript:

function createNotification(title, body, canDismiss, callback)
{
    //create the container
    var nContainer = document.createElement("div");
    nContainer.setAttribute("id", "note"+title);
    nContainer.setAttribute("class","nContainer");
    nContainer.className = "nContainer";

    //create the title
    var nTitle = document.createElement("div");
    nTitle.setAttribute("class", "nTitle");
    nTitle.className = "nTitle";
    nTitle.innerHTML = title;

    //if canDismiss is true then add controls
    if (canDismiss)
    {
        var nDismiss = document.createElement("div");
        nDismiss.setAttribute("class", "nDismiss");
        nDismiss.className = "nDismiss";
        nDismiss.innerHTML = "[close]";
        nDismiss.onclick = function(){destroyNotification(title);callback();};
        nTitle.appendChild(nDismiss);

    }

    //append the title to the container
    nContainer.appendChild(nTitle);

    //create the body and append to container
    var nBody = document.createElement("div");
    nBody.setAttribute("class", "nBody");
    nBody.className = "nBody";
    nBody.innerHTML = body;
    nContainer.appendChild(nBody);

    document.body.appendChild(nContainer);

    //fade background
    fadeIt(title);
}

function destroyNotification(title)
{
    //get the specified notification
    var note = document.getElementById("note"+title);

    //remove the notification
    document.body.removeChild(note);

    //unfade the background
    unfadeIt(title)
}

function fadeIt(title)
{
    //create a partially opaque div and append it to the document
    var Fade = document.createElement("div");
    Fade.setAttribute("id", "fade"+title);
    Fade.setAttribute("class","fade");
    Fade.className = "fade";
    document.body.appendChild(Fade);
}

function unfadeIt(title)
{
    //get the specified fade element
    var Fade = document.getElementById("fade"+title);

    //remove the specified fade element
    document.body.removeChild(Fade);
}

Я получаю ошибку document.body. Может кто-нибудь, пожалуйста, помогите?

Это HTML:

<html>
<head>
<title></title>
<script langauge="javascript" type="text/javascript" src="notification.js"> </script>
<link rel="stylesheet" type="text/css" href="notification.css" /> 
<script language="javascript" type="text/javascript">

createNotification("The Title", "Some sort of message for our body", true, function(){alert("A Callback");});


</script>
</head>
<body>
</body>
</html>

Ошибка, которую я получил от firebug:

document.body пусто

[Сбой при этой ошибке] document.body.appendChild (nContainer);

Ответы [ 3 ]

3 голосов
/ 04 сентября 2010

Вы выполняете функцию createNotification еще до загрузки страницы. Элемент body даже не существует, когда вы вызываете его.

Вызовите функцию внизу страницы или добавьте событие загрузки [ссылка] .

0 голосов
/ 04 сентября 2010

Когда вы выполняете свой код? Я имею в виду, когда методы вызывают: после загрузки документа или где-то раньше? Также я не мог понять одну строку кода в createNotification:

function nBody.innerHTML = body; 

Можете ли вы вызвать этот метод внутри тега body вместо тега head? Или вы можете использовать событие window.onload для вызова этой функции?

0 голосов
/ 04 сентября 2010

Для объекта документа существует свойство "body".

document.getElementsByTagName("body")[0].removeChild(note);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...