JS学习(1)-JavaScript in HTML

1

为了兼容各种浏览器(xhtml,html),在html 使用inline javascript时推荐的格式如下所示:

<script type=”text/javascript”>
//<![CDATA[
    function compare(a, b) {
        if (a < b) {
            alert(“A is less than B”);
        } else if (a > b) {
            alert(“A is greater than B”);
        } else {
            alert(“A is equal to B”);
        }
    }
//]]>
</script>

不过,推荐使用外部javascript脚本文件,而不是inline code。外部脚本文件有以下优势:可维护性,浏览器可以缓存,以及更好的兼容性

2

常用的Doctype声明格式

<!-- HTML 4.01 Strict -->
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”>

<!-- XHTML 1.0 Strict -->
<!DOCTYPE html PUBLIC
“-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<!-- HTML5 -->
<!DOCTYPE html>

<!-- HTML 4.01 Transitional -->
<!DOCTYPE HTML PUBLIC
“-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>

<!-- HTML 4.01 Frameset -->
<!DOCTYPE HTML PUBLIC
“-//W3C//DTD HTML 4.01 Frameset//EN”
“http://www.w3.org/TR/html4/frameset.dtd”>

<!-- XHTML 1.0 Transitional -->
<!DOCTYPE html PUBLIC
“-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<!-- XHTML 1.0 Frameset -->
<!DOCTYPE html PUBLIC
“-//W3C//DTD XHTML 1.0 Frameset//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd”>

3

<noscript>标签可以用于优雅地处理那些不支持脚本的浏览器的显示

<!DOCTYPE html>
<html>
    <head>
        <title>Example HTML Page</title>
        <script type=”text/javascriptdefer=”defer” src=”example1.js”></script>
        <script type=”text/javascriptdefer=”defer” src=”example2.js”></script>
    </head>
    <body>
        <noscript>
            <p>This page requires a JavaScript-enabled browser.</p>
        </noscript>
    </body>
</html>

4

defer 和async
defer表示脚本会立即下载带式要等html结束标签加载完时才开始执行;async表示不用等脚本加载和执行完,内容部分可以直接加载。

<!DOCTYPE html>
<html>
    <head>
    <title>Example HTML Page</title>
    <script type=”text/javascriptdefer src=”example1.js”></script>
    <script type=”text/javascriptasync src=”example2.js”></script>
    </head>
    <body>
    <!-- content here -->
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值