顯示具有 javascript 標籤的文章。 顯示所有文章
顯示具有 javascript 標籤的文章。 顯示所有文章

用javascript實作post送Xml

沒有留言:
就這樣,然後想辦法把XML字串丟進來吧!
function send() {
    var xml = '';

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "http://localhost/", true);
    xmlhttp.send(xml);
}
[1]
xmlhttp.open(用POST, 傳去哪個網址, true);
xmlhttp.send(XML字串); 測試頁面
<html>
<head>
    <title>test</title>
    <script type="text/javascript" src="test.js"></script>
</head>
<body>
    <button onclick=send()>Go!!</button>
</body>
</html>

參考資料

[1] How to send a specialized XML request in JavaScript

Javascript的封裝

沒有留言:

封裝1

宣告+定義

function Female(name, age) {
    this.name = name;
    var age = age;
    this.sayAge = function() {
        if (age > 18) {
            console.log(this.name + ' is 18.');
        }
        else
        {
            console.log(this.name + ' is ' + age + '.');
        }
    };
};

呼叫執行

var mary = new Female('Mary', 16);
mary.sayAge();

var susan = new Female('Susan', 36);
susan.sayAge();

顯示

Mary is 16.
Susan is 18.

繼承2

宣告+定義

function Female(name, age) {
    this.name = name;
    var age = age;
    this.sayAge = function() {
        if (age > 18) {
            console.log(this.name + ' is 18.');
        }
        else
        {
            //sayRealAge   is a function
            //sayRealAge() is a function return value
            console.log(this.name + ' is ' + sayRealAge() + '.');
        }
    };
    
    var sayRealAge = function() {
     return age;
    };
};

呼叫執行

var mary = new Female('Mary', 16);
mary.sayAge();

var susan = new Female('Susan', 36);
susan.sayAge();

顯示

Mary is 16.
Susan is 18.

Javascript的繼承

沒有留言:

繼承1

宣告+定義

function Animal(name) {
    this.name = name;
    this.age = 18;
    this.sayName = function() {
        console.log(this.name + ' and ' + this.age);
    };
};

function Human() {
};

//繼承: Human is a Animal
Human.prototype = new Animal('bad student');

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

bad student and 18

繼承2

宣告+定義

function Animal(name) {
    this.name = name;
    this.age = 18;
    this.sayName = function() {
        console.log(this.name + ' and ' + this.age);
    };
};

function Human() {
};

//繼承: Human is a Animal
Human.prototype = new Animal();

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

undefined student and 18

繼承3

宣告+定義

function Animal(name) {
    this.name = name;
    this.age = 18;
    this.sayName = function() {
        console.log(this.name + ' and ' + this.age);
    };
};

function Human(name) {
    this.name = name
};

//繼承: Human is a Animal
Human.prototype = new Animal('bad student');

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

good student and 18

Javascript小小入門

沒有留言:

無物件概念

文字變數

var sayHi = 'helloworld';
var sayHi = "helloworld";

數字變數

var i = 0;
var pi = 3.14;

函數

宣告+定義

var helloworld = function() {
    console.log('hello world!');
};

呼叫執行

helloworld();

顯示

hello world!

有物件概念

Javascript有一個特有的特色,稱為「原型導向」,所以在此不直接定成「物件導向」

建構式

下面兩種寫法相同。
var student = new Object();
var student = {};

建構式1

宣告+定義

function human(name) {
    this.name = name;
    this.goto = function(place) {
        console.log(this.name + ' go to ' + place);
    };
};

呼叫執行

var student = new human('good student');
student.goto("school");

顯示

good student go to school


類別加上新的 Method

function human(name) {
    this.name = name;
};

human.prototype.goto = function(place) {
    console.log(this.name + ' go to ' + place);
};

呼叫執行

var student = new human('good student');
student.goto("school");

顯示

good student go to school



建構式2

直接建構物件

var student = {
    name: 'good student',
    goto: function(place) {
        console.log(this.name + ' go to ' + place);
    }
};

呼叫執行

student.goto("school");

顯示

good student go to school


物件加上新的 Method

var student = {
    name: 'good student',
};

student.goto = function(place) {
    console.log(this.name + ' go to ' + place);
};

呼叫執行

student.goto("school");

顯示

good student go to school


javascript類似windows的messagebox

沒有留言:
javascript可以直接呼叫類似windows的messagebox的東西。
分成三種

警告視窗

alert('警告視窗, 按確定, 無回傳值');

確認視窗

confirm('確認視窗, 按確定回傳true, 按取消回傳false');

輸入視窗

prompt("輸入視窗, 回傳輸入的值", "自動載入的預設值");