初探Javascript - #01 Hello World

作为一个程序员,能写出简单有效的代码是很好的。

当程序的功能增多,逻辑变得复杂,如何将代码一块块地拼起来,讲究的就是软件架构。经过多年的实践和经验累积,我们综合了不少建立软件架构的规则和方法。

有些人常讲架构理论之类的,虽是倒背如流,到了真正写代码的时候,却是一塌糊涂。这可能是因为他们缺少实践经验,也有可能他们读书没有完全的体会。

到了WWW这个没有compilation error(编译错误)的世界,好的程序架构更显得重要。

  • 浏览器加载HTML页面的次序:从上到下(<script>加上async除外)
<!doctype html>
<html>
<head>
    <script type="text/javascript">
        // executes immediately: cannot find "_pageTitle"
        document.getElementById("_pageTitle").innerText = "A: Hello World";
    </script>
</head>
<body>
    <h1 id="_pageTitle">Loading javascript...</h1>

    <script type="text/javascript">
        // executes immediately: ok
        document.getElementById("_pageTitle").innerText = "B: Hello World";
    </script>
</body>
</html>

页面显示:

B: Hello World

  • bodyonload事件是当body里静态的element完全加载后才触发的
<!doctype html>
<html>
<head>
    <script type="text/javascript">
        // executes on body element event: onload
        function bodyOnload() {
            document.getElementById("_pageTitle").innerText = "A: Hello World";
        }
    </script>
</head>
<body onload="bodyOnload();">
    <h1 id="_pageTitle">Loading javascript...</h1>

    <script type="text/javascript">
        // executes immediately
        document.getElementById("_pageTitle").innerText = "B: Hello World";
    </script>
</body>
</html>

页面显示:

A: Hello World

其实,B: Hello World并不是没有出现,至少它闪现过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值