Show toolbar

2014年7月14日 星期一

Regular Expression Iterator

標題:使用迭代器匹配字串
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <regex> //string lib here
using namespace std;
int main(int argc, char* argv[])
{
string str = "facet normal -8.944272e-001 4.472136e-001 0.000000e+000";
regex pattern("[+-]?[0-9]\\.[0-9]+[eE][+-]?[0-9]+");
auto words_begin = sregex_iterator(str.begin(), str.end(), pattern);
auto words_end = sregex_iterator();
cout << "Found " << std::distance(words_begin, words_end) << " numbers:\n";
for (sregex_iterator i = words_begin; i != words_end; ++i) {
cout << atof( ((smatch) *i).str().c_str() ) << '\n';
}
system("pause");
return 0;
}


說明:
使用迭代器匹配字串。

Regular Expression to match IP Address

標題:使用正規表示法驗證IP
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <regex> //string lib here
using namespace std;
int main(void) {
string ip = "192.168.1.100";
smatch results;
regex pattern("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)");
if(regex_match(ip, results, pattern)) {
cout << atof( results[1].str().c_str() ) << endl;
cout << atof( results[2].str().c_str() ) << endl;
cout << atof( results[3].str().c_str() ) << endl;
cout << atof( results[4].str().c_str() ) << endl;
} else {
cout << "Not Match." << endl;
}
system("pause");
return 0;
}

說明:
簡單使用正規表示法驗證IP,並印出每個區段數值。

Bisection method using Recursion

標題:使用遞迴函式計算二分法

使用遞迴
VC++ (recursion.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Chapter 3 - Recursion
#include <iostream>
using namespace std;
const int No = 1000;
const double tolerance = 0.00001;
const double minimal = 0;
const double maximal = 1;
double equation(double);
double determine(double, double, double=equation(minimal), int=0);
int main(void)
{
cout << "Best: " << scientific << determine(minimal, maximal) << endl;
system("pause");
return 0;
}
double determine(double a, double b, double fa, int n) {
double p = 0, fp = 0;
p = a+(b-a)/2;
fp = equation(p);
//cout << dec << n << '\t' << scientific << p << endl;
if(( fp==0 || (b-a)/2 < tolerance ) || n >= No)
return p;
return (fa*fp>0) ? determine(p, b, fp, n+1) : determine(a, p, fa, n+1);
}
double equation(double x) {
return x - 1/pow(2.0, x);
}
/*double equation(double x) {
return pow(x, 4)-2*pow(x, 3)-4*pow(x, 2)-4*x+4;
}*/

不使用遞迴
VC++ (bisectionMethod.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Chapter 3 - Bisection Method
#include <iostream>
using namespace std;
const int No = 1000;
const double tolerance = 0.00001;
const double minimal = 0;
const double maximal = 1;
double equation(double);
int main(void)
{
int i=0;
double x=0, y=0, fa=0, fp=0, p=0, a=0, b=0;
a = minimal;
b = maximal;
fa = equation(a);
while(i<No) {
p = a+(b-a)/2;
fp = equation(p);
if(fp==0 || (b-a)/2 < tolerance) {
break;
}
if(fa*fp>0) {
a = p;
fa = fp;
} else {
b = p;
}
i++;
}
cout << scientific << p << endl;
system("pause");
return 0;
}
/*
double equation(double x) {
return pow(x, 4)-2*pow(x, 3)-4*pow(x, 2)-4*x+4;
}
*/
double equation(double x) {
return x - 1/pow(2.0, x);
}

說明:
比較遞迴與不遞迴方式計算二分法。

Default Argument

標題:函式預設引數
VC++ (defaultArgument.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 3 - Default Argument
#include <iostream>
using namespace std;
double circumference(double, double = 3.14);
int main(void)
{
//scientific使用科學記號輸出
cout << scientific << circumference(1) << endl;
cout << scientific << circumference(1, acos(-1.0)) << endl;
system("pause");
return 0;
}
double circumference(double d, double pi) {
return d * pi;
}
範例結果:


說明:
基礎的函式預設引數範例。

2014年7月2日 星期三

Simple Regular expression

標題:基本正規表示法
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
//#include <string>
#include <regex> //string lib here
using namespace std;
int main(void)
{
int r = 0;
string number = "";
//匹配模型
regex number_format("[0-9]{1,5}");
//隨機數
srand(time(NULL));
r = rand()%10;
cout << "Input number:" << endl;
cin >> number;
//資料比對
if(regex_match(number, number_format)) {
cout << number << " * " << r << " = " << stoi(number) * r << endl;
} else {
cout << "Not match." << endl;
}
system("pause");
return 0;
}

說明:
基本的正規表示法進行數字的匹配,並乘上一隨機數。

For statement

標題:for迴圈語句
VC++ (forstatement.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Chapter 1 - for statement
#include <iostream>
using namespace std;
int main(void)
{
int i = 0, j = 0, len = 0;
const double PI = acos(-1.0), DEGREE = 180.0/PI;
for(i=0;i<=180;i+=10) {
len = (int)( sin( i/DEGREE )*30 );
for(j=0;j<len;j++) {
cout << '*';
}
cout << " " << len << endl;
}
system("pause");
return 0;
}
範例結果:

說明:
基本使用for迴圈印出sin波範例

If...else...statements

標題:if else 判斷語句
VC++ (ifstatement.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Chapter 1 - if statement
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int len = 0;
string footballer = "";
cout << "Input Name:";
cin >> footballer;
len = footballer.length();
if(len != 0 && len <= 10) {
cout << footballer << endl;
} else {
cout << "Word length of less than 10" << endl;
}
system("pause");
return 0;
}

說明:
基本if else判斷語句的使用

Cin and cout command

標題:輸入與輸出命令
VC++ (variable.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 1 - cin and cout
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int age = 19, year = 2011;
string footballer = "";
cout << "Input Name:";
cin >> footballer;
cout << footballer << " is a Brazilian footballer.\n";
cout << "At the age of " << age << ", " << footballer <<" won the " << year << " South American Footballer\n";
system("pause");
return 0;
}

說明:
基本輸入與輸出命令

Variable type and size

標題:變數型態與大小
VC++ (datatype.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 1 - datatype
#include <iostream>
using namespace std;
int main(void)
{
cout << "type\tbyte" << endl;
cout << "int\t" << sizeof(int) << endl; //4
cout << "long\t" << sizeof(long) << endl; //4
cout << "float\t" << sizeof(float) << endl; //4
cout << "double\t" << sizeof(double) << endl; //8
cout << "char\t" << sizeof(char) << endl; //1
cout << "bool\t" << sizeof(bool) << endl; //1
system("pause");
return 0;
}

說明:
基本的變數型態與大小

Cout example

標題:cout範例
VC++ (cout.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Chapter 1 - Part1
//引用函式庫,使用標準函式庫
#include <iostream>
//標準函式庫的命名空間
using namespace std;
//主函式寫法
int main(void)
{
//印出整數及文字
cout << 10 << endl;
cout << "Neymar" << endl;
//送出Command命令pause
system("pause");
//回傳終止
return 0;
}

說明:
基礎cout範例程式

2014年5月5日 星期一

AJAX send POST, GET, FILE and get JSON

標題:AJAX送出POST/GET/FILE並取得JSON資訊
HTML (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-TW" xml:lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send Post</title>
<script type="text/javascript">
var sendPost = function() {
this.current = null;
this.queue = [];
};
sendPost.prototype.request = function(q) {
var self = this;
var query = [];
var formdata = new FormData(document.createElement('form'));
for(key in q.params) {
switch(q.params[key].constructor) {
case Number:
formdata.append(key, q.params[key]);
break;
case String:
formdata.append(key, q.params[key]);
break;
case Array:
for(var i=0;i<q.params[key].length;i++) {
formdata.append(key, q.params[key][i]);
}
break;
default:
var type = q.params[key].getAttribute("type");
if(type == 'file') {
for(var i=0;i<q.params[key].files.length;i++) {
formdata.append(key, q.params[key].files[i]);
}
} else if(type=='password'||type=='text'||type=='hidden') {
formdata.append(key, q.params[key].value);
}
break;
}
}
var xobj = new XMLHttpRequest();
xobj.overrideMimeType('application/json');
xobj.open('POST', q.url, true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == '200') {
self.current.load(JSON.parse(xobj.responseText));
self.action();
}
};
this.queue.push({
xobj : xobj,
formdata : formdata,
load : q.load
});
if(!this.current) this.action();
};
sendPost.prototype.action = function() {
this.current = null;
if(this.queue.length) {
this.current = this.queue.shift();
this.current.xobj.send(this.current.formdata);
}
};
window.onload = function() {
document.getElementById('btn').onclick = function() {
var mySendPost = new sendPost();
mySendPost.request({
url : '/test?method=work',
params : {
'file' : document.getElementById('myfile'),
'name' : document.getElementById('myname'),
'password' : document.getElementById('mypassword'),
'hidden' : document.getElementById('myhidden'),
'str' : 'Hello',
'num1' : 20,
'num2' : new Array(30, 40)
},
load : function(data) {
document.getElementById('output').innerHTML = JSON.stringify(data, null, 2);
}
});
};
};
</script>
</head>
<body>
<input type="file" id="myfile" multiple="multiple" /><br />
<input type="text" id="myname" value="QQBoxy" /><br />
<input type="password" id="mypassword" value="abcd1234" /><br />
<input type="hidden" id="myhidden" value="HideBoxy" />
<button id="btn">Button</button><br />
<pre id="output"></pre>
</body>
</html>

Node.js (main.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var express = require('./node_modules/express'),
multiparty = require('./node_modules/multiparty'),
multipart = require('./node_modules/connect-multiparty');
var port = 1337;
var app = express();
var multipartMiddleware = multipart();
app.listen(port);
app.use('/', express.static(__dirname + '/html/'));
app.post('/test', multipartMiddleware, function(req, res) {
res.set('Content-Type', 'application/json;charset=utf-8');
var json = {
'get' : req.query,
'post' : req.body,
'file' : req.files
};
//console.log(JSON.stringify(json, null, 2)); //測試用
res.send(JSON.stringify(json, null, 2));
});
console.log("Start express server on port %s", port);

範例結果:


說明:
使用FormData方式傳送GET、POST、FILE資訊,
並且建立簡單Node.js伺服器傳回之JSON資訊進行測試,
關於Node.js接收檔案的方法請參考這篇
Google Chrome 34.0.1847.131 m、Firefox 27.0.1、IE11 測試可正常執行。

2014年5月2日 星期五

A Message Board Homework

標題:留言板作業
PHP (index.php):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**************************************************
File:index.php
Name:首頁
Explain:首頁功能
****************************************By QQBoxy*/
require('session.php');
require('database.php');
require('common.php');
$output = "";
$p = !empty($_GET['p']) ? $_GET['p'] : 'signin';
$main = new main();
$output .= $main->htmlHeader();
switch($p) {
case 'install': //安裝,第一次可以把註解拿掉用來安裝,之後請把註解補回去。
//$output .= $main->install();
break;
case 'signin': //登入
$output .= $main->signin();
break;
case 'signin_jump': //登入跳轉
$output .= $main->signin_jump();
break;
case 'signup': //註冊
$output .= $main->signup();
break;
case 'signup_jump': //註冊跳轉
$output .= $main->signup_jump();
break;
case 'logout': //登出跳轉
$output .= $main->logout_jump();
break;
case 'chatroom': //聊天室
$output .= $main->chatroom();
break;
case 'profile': //聊天室
$output .= $main->profile();
break;
default:
$output .= $main->signin();
break;
}
$output .= $main->htmlFooter();
echo $output;
?>

2014年4月29日 星期二

Touch events example

標題:觸控事件範例
JavaScript (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Start: <div id="t1"></div>
Move: <div id="t2"></div>
End: <div id="t3"></div>
<script type="text/javascript">
document.ontouchstart = function(e) {
var px = e.changedTouches[0].clientX;
var py = e.changedTouches[0].clientY;
e.preventDefault();
document.getElementById('t1').innerHTML = '(' + px + ', ' + py + ')';
};
document.ontouchmove = function(e) {
var px = e.changedTouches[0].clientX;
var py = e.changedTouches[0].clientY;
e.preventDefault();
document.getElementById('t2').innerHTML = '(' + px + ', ' + py + ')';
};
document.ontouchend = function(e) {
var px = e.changedTouches[0].clientX;
var py = e.changedTouches[0].clientY;
e.preventDefault();
document.getElementById('t3').innerHTML = '(' + px + ', ' + py + ')';
};
</script>

說明:
觸控事件取得座標範例。

2014年4月13日 星期日

Servo Motor Control with an Arduino

標題:使用Arduino控制伺服馬達


Arduino (servo_motor):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Servo.h>
Servo myservo;
const int umin = 500, umax = 2400, umid = (umin + umax)/2;
const int buttonPin = 2;
int buttonState = 0, buttonDownState = 0, motorState = 0;
void setup() {
pinMode(buttonPin, INPUT);
myservo.attach(9, umin, umax);
myservo.writeMicroseconds(umid);
delay(3000);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonDownState = 1;
} else {
if(buttonDownState == 1) {
if(motorState == 0) {
myservo.writeMicroseconds(umin);
motorState = 1;
} else if(motorState == 1) {
myservo.writeMicroseconds(umid);
motorState = 2;
} else if(motorState == 2) {
myservo.writeMicroseconds(umax);
motorState = 3;
} else {
myservo.writeMicroseconds(umid);
motorState = 0;
}
buttonDownState = 0;
}
}
}

Arduino Diecimila
電阻 - 10kΩ
伺服機 - GWS S03T

說明:
按鈕依序輸出脈衝寬度最小、中間、最大,以控制依序移動伺服機位置。

2014年3月26日 星期三

C# POST using WebRequest

標題:C#使用WebRequest POST後取得回傳資料
C# (Program.cs):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net; //WebRequest WebResponse
using System.IO; //Stream
public class Global
{
public static string server = "http://127.0.0.1:1337";
}
namespace WebPostRequest
{
class Program
{
static string login(string email, string password)
{
//宣告送出資料
string param = "email=" + email + "&password=" + password;
byte[] byteArray = Encoding.UTF8.GetBytes(param);
//Request資料
WebRequest request = WebRequest.Create(Global.server + "/login");
request.Credentials = CredentialCache.DefaultCredentials;
((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
request.Method = "POST";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
//處理資料流
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//Response資料
WebResponse response = request.GetResponse();
string rawJsonString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
return rawJsonString;
}
static void Main(string[] args)
{
Console.WriteLine( login("test@gmail.com", "abcd1234") );
Console.WriteLine("\r\nPress enter key to continue....");
Console.Read();
}
}
}

說明:
簡單使用WebRequest POST送出email、password後回傳字串資料的作法。

2014年3月11日 星期二

Dynamic get jsonp

標題:動態取得Jsonp資料
JavaScript (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<div id="output_getjsonp"></div>
<script type="text/javascript">
var getjsonp = function() {
this.head = document.getElementsByTagName('head')[0] || document.documentElement;
this.current = null;
this.queue = [];
};
getjsonp.prototype.request = function(q) {
var fid = (q.fid||'_getjsonp') + new Date().getTime();
console.log(fid);
var script = document.createElement('script');
script.src = q.url.replace('callback=?', 'callback=' + fid);
this.queue.push({
fid : fid,
script : script,
load : q.load
});
if(!this.current) this.action();
};
getjsonp.prototype.action = function() {
var self = this;
this.current = null;
if(this.queue.length) {
this.current = this.queue.shift();
window[this.current.fid] = function(data) {
self.head.removeChild(self.current.script);
self.current.load && self.current.load(data);
self.action();
};
this.head.appendChild(this.current.script);
}
};
var jsonp = new getjsonp();
jsonp.request({
fid : '_myjsonp',
url : 'http://codeboxy.blogspot.com/feeds/posts/default?alt=json&callback=?',
load : function(data) {
document.getElementById('output_getjsonp').innerHTML = data.feed.entry[0].title.$t;
}
});
</script>

範例結果:
Mask String

說明:
動態使用jsonp取得json資料,測試支援版本:
  • Google Chrome 33.0.1750.146 m
  • Firefox 27.0.1
  • IE 11.0.9600.16518

2014年3月10日 星期一

JavaScript Advanced Logical Operators

標題:JavaScript邏輯運算子進階用法
JavaScript (main.js):
1
2
3
4
5
6
7
8
9
10
11
12
function foo() {
console.log('Hello');
return true;
}
function bar() {
console.log('World');
return false;
}
foo() && bar(); //Hello World
bar() && foo(); //World
foo() || bar(); //Hello
bar() || foo(); //World Hello

說明:
1. foo()為true 則會繼續執行bar()
2. bar()為false不會繼續執行foo()
3. foo()為true 不會繼續執行bar()
4. bar()為false則會繼續執行foo()

2014年3月6日 星期四

Fluent Interface in exports of Node.js

標題:Node.js的exports使用流暢介面寫法
Node.js (main.js):
1
2
3
4
var my = require('./count.js');
var foo = new my.foo(10, 2);
foo.plus().say();
foo.times().say();
exports js (count.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var foo = function(m, n) {
this.m = m;
this.n = n;
this.answer = 0;
this.method = "";
};
foo.prototype.plus = function() {
this.answer = this.m + this.n;
this.method = "+";
return this;
};
foo.prototype.times = function() {
this.answer = this.m * this.n;
this.method = "*";
return this;
};
foo.prototype.say = function() {
console.log("%d %s %d = %d", this.m, this.method, this.n, this.answer);
return this;
};
exports.foo = foo;
範例結果:


說明:
在Node.js中採用流暢介面寫法,並使用exports建立一個外部js,在主程式中require後使用。

2014年2月27日 星期四

File upload problem solving in Connect 3.0

標題:解決Connect 3.0檔案上傳的問題
Node.js (main.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Connect 3.0 的 connect.multipart() 將被移除,
可使用 multiparty 解決向下相容,方法如下:
1. 安裝multiparty
npm install multiparty
2. 安裝connect-multiparty
npm install connect-multiparty
3. 安裝qs (因 connect-multiparty 需要用到)
npm install qs
4. 建立一個uploads資料夾
By QQBoxy
*/
var express = require('./node_modules/express'),
multiparty = require('./node_modules/multiparty'),
multipart = require('./node_modules/connect-multiparty'),
fs = require('fs');
var multipartMiddleware = multipart();
var app = express();
var port = 1337;
app.listen(port);
app.post('/uploads', multipartMiddleware, function(req, res) {
fs.readFile(req.files.myfile.path, function (err, data) { //fs讀檔
var newPath = __dirname + "/uploads/" + req.files.myfile.originalFilename; //指派新路徑
fs.writeFile(newPath, data, function (err) { //fs寫檔
console.log(req.files);
res.send(req.files);
});
});
});
app.get('/', function (req, res) {
res.send( //表單
'<form action="/uploads" enctype="multipart/form-data" method="post">'+
'<input type="file" name="myfile" multiple="multiple"><br />'+
'<input type="submit" value="Upload">'+
'</form>'
);
});
console.log("Start express server");

說明:
Connect 3.0 的 connect.multipart() 將被移除,可使用 multiparty 解決向下相容的問題。

2014年1月20日 星期一

Dynamic Post with Iframe

標題:在JavaScript使用虛擬form送出post並以iframe讀取text
JavaScript (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<button id="test">test</button>
<div id="show">show</div>
<script type="text/javascript">
function $(id) {
switch(id.substr(0,1)) {
case '#':
return document.getElementById(id.substr(1));
case '.':
var elems = document.body.getElementsByTagName('*');
var target = id.substr(1);
var result=[];
for(i=0;j=elems[i];i++) {
if((j.className).indexOf(target)!=-1) result.push(j);
}
return result;
default:
return document.getElementsByTagName(id);
}
}
var loadpost = {
current : null,
queue : [],
request : function(q) {
//Create iframe
try { //IE
var iframe = document.createElement('<iframe name="loadboxy">');
} catch (e) {
var iframe = document.createElement('iframe');
}
iframe.style.display = 'none';
iframe.name = 'loadboxy';
iframe.id = 'loadboxy';
if(iframe.attachEvent) {
iframe.attachEvent("onload", function(){
loadpost.load();
});
} else {
iframe.onload = function(){
loadpost.load();
};
}
//Create form
var form = document.createElement('form');
form.target = 'loadboxy';
form.action = q.url;
form.method = 'post';
form.style.display = 'none';
for(var key in q.param) {
if(q.param[key].constructor == Array) {
for(var i=0;i<q.param[key].length;i++) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = q.param[key][i];
form.appendChild(input);
}
} else {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = q.param[key];
form.appendChild(input);
}
}
//Queue
this.queue.push({
form : form,
iframe : iframe,
flag : 0,
load : q.load
});
if (!this.current) this.action();
},
task : function() {
var iFrame = this.current.iframe;
var iFrameBody;
if (iFrame.contentDocument) //FF
iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
else if (iFrame.contentWindow) //IE
iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
this.current.load(iFrameBody.innerHTML);
},
load : function() {
if(this.current.flag == 1) {
this.task();
$('body')[0].removeChild(this.current.iframe);
$('body')[0].removeChild(this.current.form);
this.action();
} else {
this.current.flag = 1;
this.send();
}
},
action : function() {
this.current = null;
if(this.queue.length) {
this.current = this.queue.shift();
$('body')[0].appendChild(this.current.iframe);
}
},
send : function() {
$('body')[0].appendChild(this.current.form);
this.current.form.submit();
}
};
$('#test').onclick = function() {
loadpost.request({
url : "action.php",
param : {
'num1' : 10,
'num2' : 20,
'num[]' : new Array(30, 40)
},
load : function(data) {
$('#show').innerHTML = data;
}
});
};
</script>
PHP (index.php):
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$num1 = !empty($_POST['num1']) ? $_POST['num1'] : null;
$num2 = !empty($_POST['num2']) ? $_POST['num2'] : null;
$num3 = rand(0,10);
$num = !empty($_POST['num']) ? $_POST['num'] : null;
$sum = 0;
foreach($num as $value) {
$sum += $value;
}
$total = $num1 + $num2 + $num3 + $sum;
echo $num1."+".$num2."+".$num3."+".$sum."=".$total;
?>

說明:
使用純JavaScript動態在Head產生Iframe及在Body產生Form送出Post後,呼叫函式取得Text資料。