IE11不支持 onload吗?为什么我的问候语出不来

sunkess 2015-08-10 04:40:05
onload:

function greetUser() {
if (navigator.cookieEnabled)
userName = readCookie("irock_username");


if (userName)
alert("Hello " + userName + ", I missed you.");
else
alert('Hello, I am your pet rock.');

}


IE 11始终不能执行 ,初学JS 。请指教,谢谢!



<head>
<title>iRock - The Virtual Pet Rock</title>

<script type="text/javascript" src="cookie.js"></script>

<script type="text/javascript">
var userName;

function resizeRock() {
document.getElementById("rockImg").style.height = (document.body.clientHeight - 100) * 0.9;
}

function greetUser() {
if (navigator.cookieEnabled)
userName = readCookie("irock_username");

//出现问题的地方
if (userName)
alert("Hello " + userName + ", I missed you.");
else
alert('Hello, I am your pet rock.');

}

function touchRock() {
if (userName) {
alert("I like the attention, " + userName + ". Thank you.");
}
else {
userName = prompt("What is your name?", "Enter your name here.");
if (userName) {
alert("It is good to meet you, " + userName + ".");
if (navigator.cookieEnabled)
writeCookie("irock_username", userName, 5 * 365);
else
alert("Cookies aren't supported/enabled in your browser, which means I won't remember you later. I'm sorry.");
}
}
document.getElementById("rockImg").src = "rock_happy.png";
setTimeout("document.getElementById('rockImg').src = 'rock.png';", 5 * 60 * 1000);
}
</script>
</head>

<body onload="resizeRock(); greetUser();" onresize="resizeRock();">
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="rock.png" alt="iRock" style="cursor:pointer" onclick="touchRock();" />
</div>
</body>
</html>


...全文
285 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
豪情 2015-08-11
  • 打赏
  • 举报
回复
有可能是里边的代码不兼容造成的: <script type="text/javascript" src="cookie.js"></script> 然后另外改写一下就可以了:

<!doctype html>
<html>
<head>
    <title>iRock - The Virtual Pet Rock</title>

    <!--<script type="text/javascript" src="cookie.js"></script>-->

    <script type="text/javascript">
        function writeCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = name+"="+value+expires+"; path=/";
        }
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
            }
            return null;
        }
        function eraseCookie(name) {
            createCookie(name,"",-1);
        }
        var userName;

        function resizeRock(){
            document.getElementById("rockImg").style.height = (document.body.clientHeight - 100) * 0.9;
        }

        function greetUser(){
            if(navigator.cookieEnabled)
                userName = readCookie("irock_username");

            //出现问题的地方
            if(userName)
                alert("Hello " + userName + ", I missed you.");
            else
                alert('Hello, I am your pet rock.');

        }

        function touchRock(){
            if(userName){
                alert("I like the attention, " + userName + ". Thank you.");
            }
            else {
                userName = prompt("What is your name?", "Enter your name here.");
                if(userName){
                    alert("It is good to meet you, " + userName + ".");
                    if(navigator.cookieEnabled)
                        writeCookie("irock_username", userName, 5 * 365);
                    else
                        alert("Cookies aren't supported/enabled in your browser, which means I won't remember you later. I'm sorry.");
                }
            }
            document.getElementById("rockImg").src = "rock_happy.png";
            setTimeout("document.getElementById('rockImg').src = 'rock.png';", 5 * 60 * 1000);
        }
    </script>
</head>

<body onload="resizeRock(); greetUser();" onresize="resizeRock();">
<div style="margin-top:100px; text-align:center">
    <img id="rockImg" src="rock.png" alt="iRock" style="cursor:pointer" onclick="touchRock();"/>
</div>
</body>
</html>
hookee 2015-08-11
  • 打赏
  • 举报
回复
既然是在 greetUser()里出错,说明onload已经触发了。 用IE的开发者工具跟踪调试下代码,哪里报错。
zhanbaone 2015-08-10
  • 打赏
  • 举报
回复
仔细检查下代码 然后测试下 有可能是不兼容吧 换一个试试看

87,838

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧