網頁

2009年3月13日 星期五

[程式]讓你Javascript更小更正確-用JSLint檢查語法,用Packer壓縮

由於javascript是一個結構非常鬆散的物件語言,在寫javascript的時候,常常很容易出現缺失,有時候可能少了一個逗點(,),也有可能少個分號(;),結果卻依然可以執行。

詳全文

2009年3月7日 星期六

HTML原始檔中js及css後面加了"?數字"是什麼用途?

在Ruby on Rails產生的網頁原始碼中總看到assets的後面會多一段數字,像是:
<script src="/javascripts/cache/bundle.js?1236331223" type="text/javascript"></script>
現在才知道這是為了讓確保使用者能夠拿到最新的javascript或css檔案。

看到一個Plugins - Asset timestamping,會把最後修改的timestamp加到URL中,但是plugin的comment中就有人回應「Looks like assets are timestamped in Rails automatically now (v 1.1+)」,可能這是很舊的plugin了吧!

做了測試用Live HTTP Headers去看:
1.先隨便試一個js檔,如:http://wadevelop.blogspot.com/xxx.js?12345
Cotent-Length:9999
2.再開新的tab,一樣連到http://wadevelop.blogspot.com/xxx.js?12345
Cotent-Length:0
3.然後換連到http://wadevelop.blogspot.com/xxx.js?abcde
Cotent-Length:9999
證明了?後面更新timestamp,使用者就不會使用cache,而會重新載入js或css檔案。

2009年3月2日 星期一

完整的iframe auto height程式(在iframe內容頁中處理)

全部參考:
1.Iframe自動調整高度, 在子網域SubDomain的情形
2.處理iframe auto height(Iframe自適應高度)
3.處理iframe auto height(Iframe自適應高度) - 相關問題紀錄
4.完整的iframe auto height程式(在iframe內容頁中處理)
5.或許你想要 "html include html,兩種工具介紹。"


以下是一個完整的iframe auto height程式,放在iframe內容頁的網頁中即可。 看Demo
程式使用了google的AJAX Libraries API來載入jQuery,並使用jQuery來處理部份的程式。
不需要在iframe設定name或id,因為程式會找到自己的iframe。
  1. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
  2. <script type="text/javascript">
  3.   $(document).ready(function(){
  4.     iframe_auto_height(); //當文件ready時才能正確取得iframe內容的高度
  5.   });
  6.   //iframe auto height主程式
  7.   function iframe_auto_height(){
  8.     if(!this.in_site()) return;
  9.     var iframe;
  10.     $(parent.document).find("iframe").map(function(){ //找到自己的iframe
  11.       if($(this).contents().get(0).location == window.location) iframe = this;
  12.     });
  13.     if(!iframe) return;//no parent
  14.     var content_height = $("body").height()+50;
  15.     content_height = content_height < 300 ? 300 : content_height; //設定最小高度
  16.     content_height = typeof content_height == 'number' ? content_height+"px" : content_height;
  17.     iframe.style.height = content_height;
  18.     parent.iframe_auto_height(); //當下層iframe自身完成高度調整後, 再通知上層去調整高度, 直到每一層都完成高度調整.
  19.   }
  20.   //判斷是否在網頁的iframe之中
  21.   function in_site(){
  22.     if(parent != window && this.is_crosssite() == false) return(true);
  23.     return(false);
  24.   }
  25.   //判斷是否跨站(可能是別人嵌入了你的網頁)
  26.   function is_crosssite() {
  27.     try {
  28.       parent.location.host;
  29.       return(false);
  30.     }
  31.     catch(e) {
  32.       return(true);
  33.     }
  34.   }
  35. </script>