JavaScript的事件綁定(方便不支持js的時(shí)候)
首先,比如我們使用JavaScript來加強(qiáng)我們的網(wǎng)頁,但是我們要考慮到,如果用戶的瀏覽器不支持JavaScript,或者用戶disable了JavaScript的功能,那我們的網(wǎng)頁能不能正常顯示呢?例如下面的例子,
其中popUp這個(gè)函數(shù)是自定義的,新打開一個(gè)窗口來限制URL中的網(wǎng)頁。但是如果當(dāng)客戶端不支持時(shí),那這個(gè)網(wǎng)頁就不能正常工作了。所以我們在這樣做的使用,也考慮到更多,使用如下的代碼就會顯得更加合適。
<a href = "http://www.dhdzp.com" onclick = "popUp(this.href) ; return false;">
接著,作者以CSS為例子。在我們使用CSS的過程中,我們發(fā)現(xiàn),除了我們使用了<link>把CSS文件給加載進(jìn)來外,我們沒有在我們的網(wǎng)頁內(nèi)容中加入任何css相關(guān)的代碼,這樣就能很好的把structure和style分開了,即我們的css的代碼沒有侵入我們的主要代碼里面。這樣就算客戶端不知道css,但是我們的主要內(nèi)容客戶還是可以看到的,我們的內(nèi)容結(jié)構(gòu)也能在客戶那里顯示出來。所以JavaScript相當(dāng)于behavior層,css相當(dāng)于presentation層。JavaScript也能像CSS一樣做到?jīng)]有侵入性。下面是書上的一個(gè)例子。
<a href = "http://www.dhdzp.com" onclick = "popUp(this.href) ; return false;">
上面這段代碼已經(jīng)能保證在客戶端不支持JavaScript的情況下仍然可以正常的工作,但是上面的代碼中出現(xiàn)了onclick這樣的event handler。所以現(xiàn)在我們使用像CSS中的方式來完成我們所要的功能。如下:
<a href = "http://www.dhdzp.com" class = "popup">
這樣,我們能在這個(gè)頁面加載完成的時(shí)候,執(zhí)行window.onload中,來檢測哪些<a>是使用了class,然后統(tǒng)一使用popUp的方法。如下代碼
var links = document.getElementsByTagName("a");
for (var i=0 ; i<links.length ; i++) {
if (links[i].getAttribute("class") == "popup") {
links[i].onclick = function() {
popUp(this.getAttribute("href")); //Attention use this in this place. Because this is equals onClick = "popUp(this.href)"
//so we cann't use links[i].
return false;
}
}
}
這樣就能更少地侵入我們html代碼了。
最后,作者講了我們要做到向后兼容和JavaScript的最小化。向后兼容,我們可以使用類似if(document.getElementById)來測試這個(gè)方法時(shí)候存在,存在了才能使用。JavaScript代碼的最小化主要是為了減少JavaScript,這樣能加快我們網(wǎng)頁的加載。
下面我在看書的時(shí)候碰到不懂的問題,希望大蝦們能幫忙解決一下。
對于<script>應(yīng)該放在哪里?JavaScript DOM編程藝術(shù)中所說的,我們可以把<script>放在</body>之前,不要放在<head></head>里,這樣可以加快我們加載page的速度。不是很理解。
原文:
The placement of your scripts in the markup also plays a big part in initial load times. Traditionally,
we were told to always place scripts in the <head> portion of the document, but there's a problem with
that. Scripts in the <head> block the browser's ability to download additional files (such as images or
other scripts) in parallel. In general, the HTTP specification suggests that browsers download no more
than two items at the same time per hostname. While a script is downloading, however, the browser
won't start any other downloads, even on different hostnames, so everything must wait until the script
has finished.
If you're following the progressive enhancement and unobtrusive methodologies discussed earlier
in the chapter, then moving your <script> tags shouldn't be an issue. You can make your pages load
faster simply by including all your <script> tags at the end of the document, directly before the </body>
tag. When the scripts load, your window load events will still apply your changes to the document.
相關(guān)文章
JavaScript給url網(wǎng)址進(jìn)行encode編碼的方法
這篇文章主要介紹了JavaScript給url網(wǎng)址進(jìn)行encode編碼的方法,實(shí)例分析了javascript中encodeURIComponent函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03
NestJS使用class-validator進(jìn)行數(shù)據(jù)驗(yàn)證
本文將通過詳細(xì)的步驟和實(shí)戰(zhàn)技巧,帶大家掌握如何在NestJS中使用class-validator進(jìn)行數(shù)據(jù)驗(yàn)證,以及11條實(shí)戰(zhàn)中常用的驗(yàn)證技巧,感興趣的可以了解下2024-11-11
UniApp使用manifest.json應(yīng)用配置的超詳細(xì)教學(xué)
這篇文章主要給大家介紹了關(guān)于uni-app應(yīng)用配置manifest.json最全最詳細(xì)配置,manifest.json文件是應(yīng)用的配置文件,用于指定應(yīng)用的名稱、圖標(biāo)、權(quán)限等,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
LayUI動(dòng)態(tài)設(shè)置checkbox不顯示的解決方法
今天小編就為大家分享一篇LayUI動(dòng)態(tài)設(shè)置checkbox不顯示的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
如何在JavaScript實(shí)現(xiàn)Blob文件流下載
在JavaScript中可以使用瀏覽器提供的Blob對象和URL.createObjectURL()方法來實(shí)現(xiàn)文件流下載,這篇文章主要給大家介紹了關(guān)于如何在JavaScript實(shí)現(xiàn)Blob文件流下載的相關(guān)資料,需要的朋友可以參考下2024-05-05

