Posted on 2009-12-17 17:40
terryxue 閱讀(2105)
評論(0) 編輯 收藏 所屬分類:
javascript
有時我們可能想得到某網站上的表單提交時向服務器發送的數據,最簡單的案例:在google搜索頁面上點擊"google搜索"按鈕時,頁面向服務器發送了什么?
我們可以分以下幾步來完成:
1. 在頁面中導入protoype.js
首先我們需要使用firefox來瀏覽網頁,然后安裝上firebug插件。
裝好后通過firebug在頁面中引入prototype.js,運行如下代碼:
var head = document.documentElement.firstChild
var script = document.createElement("script");
script.src= "http://prototypejs.org/assets/2009/8/31/prototype.js";
head.appendChild(script)
以上prototype的地址可能在你使用時已經修改,你可能需要修改其URL
等到prototype加載完畢后,再使用prototype的功能分析表單,以google為例,表單的名稱為"f",
我們可以通過document.f來得到表單,然后可以使用:document.f.serialize()來得到表單的提交時的數據。
當然如果點提交按鈕時,可能會有按鈕動作改變表單內的某些數據,這樣的話我們可以給表單加一個submit事件,然后在事件中通過document.f.serialize()來獲取數據,這種情況下代碼如下:

document.f.observe("submit", function(event)
{
alert(document.f.serialize());
event.stop()
});