JavaScript Reference Select and Option objects 用法介紹

在網頁裡面用 Select 是很常遇到的,之前也寫了一篇如何利用 jQuery 動態增加 option 或取值jQuery 部份就不介紹了,那是需要搭配 jQuery Plugin: Select box manipulation,今天要介紹的是如何用 javascript 動態取值或者是增加 option 選項。因為我發現有使用者直接利用 innerHtml 的方式來把資料塞入到 Select 裡面,雖然 FireFoxChrome 都可以正常運作,但是遇到 IE 還是沒辦法動。

如何取得 select element 底下很多方法可以取得 select element: 1. 透過 form name + element name

document.myform.selectname
2. 透過 form name + element 陣列(注意看 select 是位在 form element index 值多少)
document.myform.elements
3. 透過獨一無二的 ID
document.getElementById("selectid")
[Read More]

無痛安裝 NodeJS 和 Node Framework Express

nodejs-light

NodeJS 是目前當紅的 Web 2.0 技術,去年 COSCUP 2010 就有 KKBOX 資深工程師 ericpi 來探討這個議題,NodeJS 背後使用了 V8 引擎為基礎,沒看過用純 JS 來當 Server-Side 吧,台灣很紅的 Plurk 也是大量使用 NodeJS,然而每開發一種語言,就會想開始找搭配的 Framework,那就首推 Node Framework Express 來撰寫程式,本篇是要介紹如何在 Ubuntu 10.10 無痛安裝 nodejs + express。

下載 Nodejs 原始碼 直接到

官網下載 Stable 的版本吧,目前是 node-v0.4.10.tar.gz,也可以先看看 API Document

# wget http://nodejs.org/dist/node-v0.4.10.tar.gz
[Read More]

用 js 或 php 判斷 Android 手機上網

之前寫了一篇 jQuery 偵測瀏覽器版本, 作業系統(OS detection),這次可以來加上 Android 手機版本,其實就是分析瀏覽器 User Agent 來達到目的,底下分享 PHP 跟 Javascript 作法

PHP 方法

if(stripos($_SERVER['HTTP_USER_AGENT'],'Android') !== false) 
{
	header('Location: http://android.xxx.com');
	exit();
}

Javascript 方法

if(navigator.userAgent.match(/Android/i))
{
	window.location = 'http://android.xxx.com';
}

Apache .htaccess 方法 用

Apache mod rewrite 方法

RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
RewriteRule ^(.*)$ http://android.xxx.com [R=301]

jQuery 偵測瀏覽器版本, 作業系統(OS detection)

update: 簡易版的偵測 iphone/ipod time: 23:32 jQuery 真是一個相當方便的 javascript framework,最近在弄嵌入式系統時候需要去偵測瀏覽器 user agent,就類似下此訊息 “Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)",原本打算直接用 C 語言內建的 getenv(“HTTP_USER_AGENT”) 來做掉,不過後來想想,直接在 UI 那邊,利用 jQuery 來偵測瀏覽器版本、系統OS,這樣就解決了,上網找到有人寫了 jQuery browser and OS detection plugin,利用底下語法就可以知道一些 user agent 裡面的資料


    

[Read More]

[筆記] iframe 父頁子頁呼叫函式 parent call function

紀錄 iframe 如何呼叫子頁或者是父頁函式,iframe 在現今 Web 2.0 時代已經不流行了,因為有很多問題的存在,例如對於 SEO 搜尋引擎也沒有幫助,但是也是很多人在使用,底下筆記心得,說不定之後會 google 到自己的文章,哈哈。 父頁(主視窗)呼叫子頁函式:

/* iframeID 是 iframe ID*/
window.iframeID.formSubmit();
/* ifr 是 iframe ID */
document.getElementById('ifr').contentWindow.formSubmit();
子頁(iframe)呼叫父頁(主視窗)函式:
parent.formSubmit();
如果有兩層
parent.parent.formSubmit();
注意 timing issue,等 iframe 視窗 load 完之後才可以呼叫 iframe function。至於如果取主視窗跟 iframe 的變數 value,可以利用 jQuery $("#ID") 方式來得到。 reference:

【程式】JS : parent , iframe function call Access child function from iframe

[jQuery] 解決 IE6 PNG 透明背景 (Supersleight jQuery Plugin for Transparent PNGs in IE6)

今天無意間看到 Drew McLellan 在 2007 年寫了這篇 Transparent PNGs in Internet Explorer 6,真的是太晚發現這篇了,之前自己寫到一篇:『[CSS] IE 6, 7, 8 FireFox hack 支援透明背景圖 background or img javascript』,雖然 Google 官方網站宣佈完全不支援 IE6 瀏覽器,打算只支援 Microsoft Internet Explorer 7.0+, Mozilla Firefox 3.0+, Google Chrome 4.0+, Safari 3.0+,可是我們這些 Web Developer 還是需要考慮客戶的瀏覽器阿,因為客戶才是最大的,尤其是在一些學術機構,安裝好 XP,預設就是 IE6,從 Google 分析裡面,IE6 也是網站的客戶大群。

先來介紹 Drew McLellan 寫的一支好用 js 來改善所有 png 透明圖檔,最主要是修正 background-image 跟 img tag 所包含的 png 圖檔

  1. 先下載:Download SuperSleight,解壓縮放到 js 資料夾

  2. 針對 IE6 瀏覽器寫入 html

<pre class="brush: xml; title: ; notranslate" title=""><!--[if lte IE 6]>

<![endif]–>

[Read More]

[jQuery] AjaxFileUpload : Multiple File Upload plugin

最近 survey 一些 AJAX upload plugin by jQuery,或者是一些網路知名 upload opensource: SWFUpload, 以及目前很強大的 Plupload,先來說明 AjaxFileUpload 這個 jQuery Plugin 單一檔案上傳,如果想要簡單方便的單一上傳,我很推薦這個,搭配回傳的 json type 吐出錯誤訊息還蠻好用的,雖然作者給單一上傳的說明,不過還是可以將它改成多檔上傳,也就是多增加 input type=“file” 就可以了,底下介紹一下怎麼實作單一檔案或者是多檔案上傳。

單一檔案上傳 先 include AjaxFileUpload javascript

html:
jQuery code:
function ajaxFileUpload()
{
	//這部份可以省略,或者是撰寫當 ajax 開始啟動需讀取圖片,完成之後移除圖片
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	
  /*
    prepareing ajax file upload
    url: the url of script file handling the uploaded files
    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
    dataType: it support json, xml
    secureuri:use secure protocol
    success: call back function when the ajax complete
    error: callback function when the ajax failed
  */
	$.ajaxFileUpload
	(
		{
			url:'doajaxfileupload.php', 
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						alert(data.msg);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	
	return false;

}
注意 fileElementId 對應到 input file 裡面的 ID 值,取 name 是後端程式需要用到,例如 PHP $_FILES,後端處理回傳 json Type 給 jQuery 處理,json 格式:
{
  "error" : 'test',
  "msg" : 'upload completed'
}
這樣大致上ok了。如果要多檔案上傳,其實就是改 jQuery 部份:html 部份請加上多個 input file

多重檔案上傳

jQuery 部份改成:
$().ready(function() {  
    $('#uploadfile').click(function(){      
      $('input:file').each(function(){		  
        $.ajaxFileUpload
    		(
    			{
    				url:'doajaxfileupload.php', 
    				secureuri:false,
    				fileElementId: $(this).attr('id'),
    				dataType: 'json',
    				success: function (data, status)
    				{
  						if(data.error != '')
  						{
  							alert(data.error);
  						}else
  						{
  							alert(data.msg);
  						}
    				},
    				error: function (data, status, e)
    				{
    					alert(e);
    				}
    			}
    		); 
     });
     
    });
  });
另外也可以參考 jQuery Plugin:

jQuery Multiple File Upload Plugin v1.47

Google Chrome 支援超過 40,000 Extensions! with Greasemonkey

看到 Google Chrome Blog 發表的Google Chrome 支援超過 40,000 Extensions!,當 Google Chrome 瀏覽器剛出來的時候,造成 Web Developer 一些震撼,因為 Chrome 強調的是擁有快速的 Javascript 引擎,以及快速的啟動,Fast start-up、Fast loading、Fast search,也因此讓很多設計網站的工程師必須把 Chrome 的支援性考慮進去,但是由於剛推出的瀏覽器,沒有任何外掛功能,我本身用 FireFox 瀏覽器很多年了,FireFox 的附加元件讓許多程式設計師投入開發,也製造出很多方便的附加元件來讓大家使用,例如:FireBugGmail ManagerGreasemonkey…,然而 FireFox 最方便的就是 Greasemonkey 此附加元件,使用者可以撰寫簡單 Javascript 語言來跟指定網站進行元件控制,現在 Google 工程師聽到我們的聲音了,Google Chrome 4 加入 Greasemonkey user scripts 功能,大家可以到 userscripts.org 下載超過 40,000 script 安裝到 Chrome 瀏覽器。您可以在 blogger 使用 emoticons,大家可以去參考看看。 由於 Chrome 支援了 Greasemonkey,所以趕快把 FireFox 所安裝的 script,也安裝到 Chrome,可是我發現之前 DarkKiller 大神寫的 Wretch Album Expander 已經不能用了,所以我將它實做到 Chrome,可以從這裡下載安裝:Wretch Album Expander for Google Chrome or FireFox,平時自己偶而會看看無名小站,所以也是方便自己觀看照片,此 script 也可以安裝在 FireFox 喔。這樣大家就不用再看圖片還要一張一張慢慢點,只要負責按換頁就可以了 ^^。 來測試看看,隨便找一本無名相簿:馬甲‧小葵 ,畫面:點我觀看

Using firebug for firefox 除錯 javascript

在 Web 程式設計,不管是 html 或者是 CSS、甚至 javascript,都可以利用 FireFox Plugin: firebug 來除錯,順便推薦另一套 Web Develper 工具:Web Developer 1.1.8,這兩套都可以玩看看,在網路看自己東華電機學長 gasolin 寫過一篇:3 分鐘學會用 firebug 除錯 ,裡面有一個影片,建議大家看看:影片,如何利用 firebug 來對 javaascript 除錯,介紹了 firebug 優點。底下整理我看到的內容

1. 利用 console.log() 來針對變數除錯 以往都是利用 window.alert() 的方式來看看變數是否正確,現在只要在 javascript 裡面加入 console.log() 針對不同變數取值出來觀看

輸出會顯示:
a is test
[1, 2, 3, 4]

2. 印出有圖示的訊息 console.info/console.warn/console.error

這功能跟 console.log 差不多,只有差在前面有圖示符號,請看下圖:

firebug_01 (by appleboy46)

[Read More]

[Javascript] 在函數裡設定參數預設值

在網路上看到一篇:『Setting default values for missing parameters in a Javascript function』,提到在 Javascript 函式參數如果未定義,就會出現 undefine 的錯誤訊息,請看底下範例:

function foo(a, b, c) {
    document.write('a: ' + a + ' ');
    document.write('b: ' + b + ' ');
    document.write('c: ' + c + ' ');
    document.write('
'); }
測試函數:
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
輸出結果:
a: undefined b: undefined c: undefined
a: 1 b: undefined c: undefined
a: 1 b: 2 c: undefined
a: 1 b: 2 c: 3
底下有兩種方式可以解決此問題: 1. 加入 if 判斷:
function foo(a, b, c) {

    if(typeof a == 'undefined') {
        a = 'AAA';
    }
    if(typeof b == 'undefined') {
        b = 'BBB';
    }
    if(typeof c == 'undefined') {
        c = 'CCC';
    }

    document.write('a: ' + a + ' ');
    document.write('b: ' + b + ' ');
    document.write('c: ' + c + ' ');
    document.write('
'); }
測試:
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
結果輸出:
a: AAA b: BBB c: CCC
a: 1 b: BBB c: CCC
a: 1 b: 2 c: CCC
a: 1 b: 2 c: 3
2. 網友提供的最佳解法:
function foo(a, b, c) {

    a = a || "AAA";
    b = b || "BBB";
    c = c || "CCC";

    document.write('a: ' + a + ' ');
    document.write('b: ' + b + ' ');
    document.write('c: ' + c + ' ');
    document.write('
'); }
假設 a 尚未被定義,就會以 AAA 預設值顯示,程式碼也相當好閱讀。