0

我正在尝试在页面加载时运行一个函数:

<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 事件来调用它。感谢您的任何见解!

4

3 回答 3

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";
}

您缺少)腹部.style.padding.bottom 上的最后一个。

错误:预期 ')' 与第 22 行中的 '(' 匹配,但看到的是 ';'。

于 2013-10-15T21:27:36.023 回答
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 函数时会出现问题。

于 2013-10-15T21:48:49.020 回答
-1

你需要引用函数fe

<body onload="startUp">

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

于 2013-10-15T21:19:22.003 回答