一个 JavaScript 最佳实践的例子

一个 JavaScript 最佳实践的例子

作者:Grey

原文地址: 一个 JavaScript 最佳实践的例子

举个例子:

用户在点击某个链接的时候需要弹出一个新窗口

方法1. 采用"javascript:"伪协议

代码清单:

jsbestpractise1.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>JS Best Practice</title>
</head>

<body>
    <a href="javascript:popUp('http://www.baidu.com');">Example</a>
    <script src="js/jsbestpractise1.js"></script>
</body>

</html>

js/jsbestpractise1.js

function popUp(winURL) {
    window.open(winURL, "popup", "width=320,height=480");
}

这种方式在支持"javascript:"伪协议的浏览器中运行正常,但是禁用了JavaScript功能的浏览器会什么也不做。所以,这种调用方式并不好。

方法2. 通过onclick方法来触发弹出链接:

代码清单:

jsbestpractise2.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>JS Best Practice</title>
</head>

<body>
    <a href="#" onclick="popUp('http://www.baidu.com/');return false;">Example</a>
    <script src="js/jsbestpractise2.js"></script>
</body>

</html>

js/jsbestpractise2.js

function popUp(winURL) {
    window.open(winURL, "popup", "width=320,height=480");
}

这种方式对于禁用了JavaScript功能的浏览器同样什么也不做。

所以,这种调用方式也不好。

优化1

我们可以在链接的href属性中设置为真实存在的URL地址,让它成为一个有效的链接,这样,即便浏览器禁用了JavaScript,也可以通过链接直接到目标地址,好过什么都不做。

代码清单:

jsbestpractise3.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>JS Best Practice</title>
</head>

<body>
    <a href="http://www.baidu.com/" onclick="popUp('http://www.baidu.com/');return false;">Example</a>
    <script src="js/jsbestpractise3.js"></script>
</body>

</html>

js/jsbestpractise3.js

function popUp(winURL) {
    window.open(winURL, "popup", "width=320,height=480");
}

我们还可以把链接简化一些:

jsbestpractise4.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>JS Best Practice</title>
</head>

<body>
    <a href="http://www.baidu.com/" onclick="popUp(this.href);return false;">Example</a>
    <script src="js/jsbestpractise3.js"></script>
</body>

</html>

优化2

分离JavaScript,类似style属性,onclick方法也是一种既没有效率又容易引发问题的做法,

如果我们用类似CSS机制中的class属性来分离JavaScript代码和HTML页面,网页就会健壮的多。

代码清单:

jsbestpractise5.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>JS Best Practice</title>
</head>

<body>
    <a href="http://www.baidu.com/" class="popUp">Example</a>
    <script src="js/jsbestpractise5.js"></script>
</body>

</html>

js/jsbestpractise5.js

window.onload = prepareLinks;
function prepareLinks() {
    var links = document.getElementsByTagName("a");
    if (links.length > 0) {
        for (var i = 0; i < links.length; i++) {
            if (links[i].getAttribute("class") == "popUp") {
                links[i].onclick = function () {
                    popUp(this.getAttribute("href"));
                    return false;
                }
            }
        }
    }
}

function popUp(winURL) {
    window.open(winURL, "popup", "width=320,height=480");
}

这种方式的步骤如下:

  1. 获取所有链接:document.getElementsByTagName("a");

  2. 遍历链接,如果某个链接的class=popup,就表示这个链接在被点击的时候应该调用popUp函数。

优化3

检测浏览器是否支持某些JavaScript方法,如果不支持,则不执行相应的JS方法,比如:

这个例子中用到了getElementsByTagName这个方法,我们可以在执行这个方法之前,检测一下

浏览器是否支持这样的方法:

代码清单:

js/jsbestpractise7.js

window.onload = prepareLinks;
function prepareLinks() {
    if (!document.getElementsByTagName) {
        return false;
    }
    var links = document.getElementsByTagName("a");
    if (links.length > 0) {
        for (var i = 0; i < links.length; i++) {
            if (links[i].getAttribute("class") == "popUp") {
                links[i].onclick = function () {
                    popUp(this.getAttribute("href"));
                    return false;
                }
            }
        }
    }
}

function popUp(winURL) {
    window.open(winURL, "popup", "width=320,height=480");
}

自此,我们就完成了对这个例子的一些代码优化,当然,还有更进一步的优化,

比如:压缩脚本,本文暂不作说明。

代码清单

hello-js

参考资料

JavaScript DOM编程艺术(第2版)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreyZeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值