user2182164
user2182164

Reputation: 23

window.onload and getelementbyid returns null

I have a problem with my JS-onload function. The getElementById()-function returns NULL every time, but the div exists. I cannot see the bug...

Here is the HTML-code:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link rel="stylesheet" media="screen" href="styles/screen.css" />
    <script type="text/javascript" src="scripts/browser.js"></script>
    <script type="text/javascript" src="scripts/style.js"></script>
    <script type="text/javascript" src="scripts/state.js"></script>
</head>
<body>
    <div id="header"></div>
</body>
</html>

And the js-file which triggers the bug:

function onLoad() {
var element;

Browser.detect();
alert('Engine: ' + Browser.browser);

for (var i = 0; i < Style.configs.length; i++) {
    for (var c = 0; c < Style.configs[i].setup.length; c++) {
        if (Browser.browser == Style.configs[i].setup[c].engine) {
            element = document.getElementById(Style.configs[i].setup[c].size.flag);
            element.style.height = Style.configs[i].setup[c].size;
            break;
        }
        else if (Style.configs[i].setup[c].engine == "Default") {
            element = document.getElementById(Style.configs[i].setup[c].size.flag);
            element.style.height = Style.configs[i].setup[c].size;
            return;
        }
    }
}
}

window.onload = onLoad;

And at last, the style.js-file:

var Style = {
configs : [
    {
        flag : "header",
        setup : [
            {
                engine : "Gecko",
                size : 37
            },
            {
                engine : "Default",
                size : 38
            }
        ] 
    }
]
};

Upvotes: 0

Views: 1659

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

You should be using

document.getElementById(Style.configs[i].flag);

instead of

document.getElementById(Style.configs[i].setup[c].size.flag)

Upvotes: 1

Related Questions