JavaScript函数无法加载

时间:2013-10-15 21:14:17

标签: javascript onload

我正在尝试在pageload运行一个函数:

<script type="text/javascript" src="functions.js"></script>
<body onload="startUp();">

问题是startUp()没有运行,也没有我尝试在我的JS文件中引用的其他内容。我确信JS文件的链接是正确的;即使它不是,我甚至尝试将所有的functions.js粘贴到页面标题中 - 仍然没有。这是functions.js的内容:

function startUp() {
    document.write("running"); //for debugging
    alert("running"); //for debugging
    stretchAbdomen();
    if (defaultStyle()) {
        setStyleCookie(1, false);
    }
    else { //if mobilestyle
        setStyleCookie(0, false);
        if (!window.location.hash) {
            window.location.hash = "#mobilearea";
        }
    }
}

function stretchAbdomen() {
    var bodyheight = getbodyheight();
    var abdomen = document.getElementById('abdomen');
    if (window.innerHeight > bodyheight) {
        var currentpaddingstring = window.getComputedStyle(abdomen, null).getPropertyValue('padding-bottom');    
        var currentpadding = Number(currentpaddingstring.substring(0, currentpaddingstring.length - 2)); //-2 removes "px" from string
        abdomen.style.paddingBottom = (((window.innerHeight - bodyheight) + currentpadding) + "px";
    }
}

function getbodyheight() {
    var body = document.body,
    html = document.documentElement;
    return Math.min( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
}

/*** BELOW HERE SHOULD NOT BE RELEVANT FOR THE QUESTION ***/

window.onresize = resetStyleCookie; 

function defaultStyle() {
    if (window.getComputedStyle(document.getElementById('mobilearea')).getPropertyValue('width') == "1px")
        return true;
    else return false;
}

function resetStyleCookie() {
    document.cookie = "stylecookie=" + "; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
    setStyleCookie(defaultStyle() ? 1 : 0, true); //forcereset because otherwise it wasn't working on subpages
}

function setStyleCookie(number, forcereset) {
    if (document.cookie.indexOf("stylecookie") == -1 || forcereset) {
        var now = new Date();
        var time = now.getTime();
        time += 3600 * 150000;
        now.setTime(time);
        document.cookie = "stylecookie=" + number + "; expires=" + now.toGMTString() + "; path=/"; 
    } 
}

我必须假设在functions.js中存在一些编译时问题,但我的调试工具没有显示任何错误,我也无法找到任何错误。对startUp()的调用什么都不做,即使我不依赖onload事件来调用它。感谢您的任何见解!

3 个答案:

答案 0 :(得分:1)

您的JS代码中有错误。尝试使用JSLint.com验证您的代码。

if (window.innerHeight > bodyheight) {
    var currentpaddingstring = window.getComputedStyle(abdomen, null).getPropertyValue('padding-bottom');    
    var currentpadding = Number(currentpaddingstring.substring(0, currentpaddingstring.length - 2)); //-2 removes "px" from string
    abdomen.style.paddingBottom = (((window.innerHeight - bodyheight) + currentpadding) + "px";
}

您错过了abdomen.style.padding.bottom上的最后一个)

错误:预期')'匹配'('从第22行开始,而不是看到';'。

答案 1 :(得分:0)

<html>

<head>

<script type="text/javascript">

function startUp() {
document.write("running"); //for debugging
alert("running"); //for debugging

//stretchAbdomen();
if (defaultStyle()) {
    setStyleCookie(1, false);
}
else { //if mobilestyle
    setStyleCookie(0, false);
    if (!window.location.hash) {
        window.location.hash = "#mobilearea";
    }
}
}

</script>

</head>

<body onload="startUp();">

</body>

</html>

以上代码适合我。正如您所看到的,我只使用了您要引用的函数并将其放在脚本标记中 - 但它确实有效。在我看来,当您尝试引用stretchAbdomen函数时会出现问题。

答案 2 :(得分:-1)

您需要引用该函数,例如

<body onload="startUp">

确保在定义之后引用它(正如您在示例中使用外部JavaScript所做的那样)。