前言
幾乎現代的網頁都以ajax作為主要的設計方式,過去的form表單元素使用的機會越來越少,使用『非同步』ajax帶給我們許許多多的好處,例如傳送一個請求(request)到伺服器我們不需要像過去乖乖地等待回應(response),因為它會在背後執行,當它完成時會自己呼叫callback函數,這樣更帶來了更好的用戶體驗。
The script tag method for cross-domain data fetching: JSON with Padding, or simply JSONP. - Bob IppolitoJSONP也就是JSON with Padding,意思就是讓JSON資料添加了一些添加物(也就是function name),主要用來解決跨域資源存取的問題,用JSONP抓到的資料並不是JSON,而是一段javascript function並包含了JSON資料的程式碼,瀏覽器在取得後不是使用JSON解析器而使用JavaScript執譯器執行並得到結果。
var naturalWidth = $("#my_id")[0].naturalWidth;
var naturalHeight = $("#my_id")[0].naturalHeight;
var value =!!null; // value = false
var q1 = undefined;
var q2 = null;
var q3 = [];
var q4 = {};
var q5 = 0;
var q6 = 1;
var q7 = 999;
var q8 = "";
var q9 = "hello";
var q10 = false;
var q11 = true;
var q12 = new Boolean("true");
var q13 = new Boolean("false");
var q14 = new Number(0);
var q15 = new Number(1);
var a1 = !!q1;
var a2 = !!q2;
var a3 = !!q3;
var a4 = !!q4;
var a5 = !!q5;
var a6 = !!q6;
var a7 = !!q7;
var a8 = !!q8;
var a9 = !!q9;
var a10 = !!q10;
var a11 = !!q11;
var a12 = !!q12;
var a13 = !!q13;
var a14 = !!q14;
var a15 = !!q15;
a1 = false
a2 = false
a3 = true
a4 = true
a5 = false
a6 = true
a7 = true
a8 = false
a9 = true
a10 = false
a11 = true
a12 = true
a13 = true
a14 = true
a15 = true
你答對了幾題呢!?
typeof 0; // "number"
typeof false; // "number"
typeof new Boolean("false"); // "object"
typeof new Number(0); // "object" | Input Type | Result |
|---|---|
| Undefined | false |
| Null | false |
| Boolean | The result equals the input argument (no conversion). |
| Number | The result is false if the argument is +0, -0, or NaN; otherwise the result is true. |
| String | The result is false if the argument is the empty string (its length is zero); otherwise the result is true. |
| Object | true |
// anit-pattern
// infinite loop
var divs = document.getElementsByTagName('div');
for(var i = -; i < divs.length; i++){
var div = doucument.createElement('div');
document,body.appendChild(div);
}
var divs = document.getElementsByTagName('div');
var length = divs.length;
for(var i = -; i < length; i++){
var div = doucument.createElement('div');
document,body.appendChild(div);
}
var arr = Array.prototype.slice.call( htmlCollection );
http://padolsey.net/p/Sonic/repo/demo/demo.html
<!doctype html>
<meta charset="utf-8" />
<title>Sonic</title>
<style>
</style>
<body>
<script src="../src/sonic.js"></script>
<script>
var square = new Sonic({
width: 100,
height: 100,
fillColor: '#000',
path: [
['line', 10, 10, 90, 10],
['line', 90, 10, 90, 90],
['line', 90, 90, 10, 90],
['line', 10, 90, 10, 10]
]
});
square.play();
document.body.appendChild(square.canvas);
</script>
</body>
var circle = new Sonic({
width: 50,
height: 50,
padding: 50,
strokeColor: '#000',
pointDistance: .01,
stepsPerFrame: 3,
trailLength: .7,
step: 'fader',
setup: function() {
this._.lineWidth = 5;
},
path: [
['arc', 25, 25, 25, 0, 360]
]
});
circle.play();
document.body.appendChild(circle.canvas);
$.fn.imagesLoaded = function(callback){
var elems = this.filter('img'),
len = elems.length,
blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
elems.bind('load.imgloaded',function(){
if (--len <= 0 && this.src !== blank){
elems.unbind('load.imgloaded');
callback.call(elems,this);
}
}).each(function(){
// cached images don't fire load sometimes, so we reset src.
if (this.complete || this.complete === undefined){
var src = this.src;
// data uri bypasses webkit log warning (thx doug jones)
this.src = blank;
this.src = src;
}
});
return this;
};
Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的系統中而不同。
$('#my-container').imagesLoaded( function( $images, $proper, $broken ) {
// callback provides three arguments:
// $images: the jQuery object with all images
// $proper: the jQuery object with properly loaded images
// $broken: the jQuery object with broken images
// `this` is a jQuery object of container
console.log( $images.length + ' images total have been loaded in ' + this );
console.log( $proper.length + ' properly loaded images' );
console.log( $broken.length + ' broken images' );
});
<body>
<div class="wrap">
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy7FTlB5bSSvupxU0BJ8nGe9jOP5YMLfyD_m_i88W9QC3G4atjy1qMuo5J-Nyk5TgzLGJZocLt9xR139ObeKz98zsdjWzX8KJyPr2Z4PUIewqHi4frJ9xNd_9mhdaMbq9aOve6_OqNO9tX/s1600/finish_line_970825.jpg" />
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy7FTlB5bSSvupxU0BJ8nGe9jOP5YMLfyD_m_i88W9QC3G4atjy1qMuo5J-Nyk5TgzLGJZocLt9xR139ObeKz98zsdjWzX8KJyPr2Z4PUIewqHi4frJ9xNd_9mhdaMbq9aOve6_OqNO9tX/s1600/finish_line_970825.jpg" />
<img src="http://2.bp.blog" />
</div>
</body>
3 images total have been loaded in [object Object]
2 properly loaded images
1 broken images
$(".wrap img").each(function(){
$(this).imagesLoaded(function(){
console.log('loaded!!');
});
});
loaded!!
loaded!!