Skip to Content
JavaScript 設計模式
book

JavaScript 設計模式

by Stoyan Stefanov
March 2012
Intermediate to advanced
244 pages
4h 42m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 設計模式
DOM 和瀏覽器的模式
   
203
這個
ttt
物件保有一份已填過的格子清單,儲存在
ttt.played
,並會傳送給伺服器,
這樣伺服器就可以回傳一個尚未填過的數值。若是有錯誤發生,伺服器的回應則會類似
這樣:
如你所見,JSONP 裡頭的回呼,必須是一個公開且可以全域存取的函式,倒不一定要是
全域函式,但可以是一個全域物件的方法。而若是沒有錯誤,伺服器回應的方法呼叫會類
似這個樣子:
這裡 3 的意思是伺服器選擇了編號 3 的格子。在這個案例中資料相當簡單,所以我們不需
要用 JSON 格式,一個值就足夠了。
頁框和 Image Beacons
頁框是另一種遠端操作的選擇。你可以用 JavaScript 建立一個
iframe
元素並變更其
src
URL。新的 URL 中可以包含資料和函式呼叫,並更新原呼叫者,也就是在
iframe
面的父頁面。
remote scripting 中最簡單的格式,會出現在當你需要的只是傳送資料給伺服器,而不需
要任何回應的時候。在這種情況,你可以建立一個新的圖片,並將它的
src
指向伺服器端
script
這個模式稱為 image beacon,當你想要傳送資料讓伺服器記錄時很有用,例如,收集訪客
統計數據。因為回覆的 beacon對你來說毫無用處,一種常見但不好的作法是,讓伺服器回
覆一個 1x1 大小的 GIF 圖檔。更好的選擇是,回應「204 沒有內容」的 HTTP 回應。這意
思是送回給客戶端的只有標頭,但沒有內容主體。[19 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript 之美

JavaScript 之美

Anton Kovalyov
重構 JavaScript

重構 JavaScript

Evan Burchard

Publisher Resources

ISBN: 9789862764190