Posted on 2009-12-17 17:40
terryxue 閱讀(2108)
評(píng)論(0) 編輯 收藏 所屬分類:
javascript
有時(shí)我們可能想得到某網(wǎng)站上的表單提交時(shí)向服務(wù)器發(fā)送的數(shù)據(jù),最簡(jiǎn)單的案例:在google搜索頁面上點(diǎn)擊"google搜索"按鈕時(shí),頁面向服務(wù)器發(fā)送了什么?
我們可以分以下幾步來完成:
1. 在頁面中導(dǎo)入protoype.js
首先我們需要使用firefox來瀏覽網(wǎng)頁,然后安裝上firebug插件。
裝好后通過firebug在頁面中引入prototype.js,運(yùn)行如下代碼:
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的地址可能在你使用時(shí)已經(jīng)修改,你可能需要修改其URL
等到prototype加載完畢后,再使用prototype的功能分析表單,以google為例,表單的名稱為"f",
我們可以通過document.f來得到表單,然后可以使用:document.f.serialize()來得到表單的提交時(shí)的數(shù)據(jù)。
當(dāng)然如果點(diǎn)提交按鈕時(shí),可能會(huì)有按鈕動(dòng)作改變表單內(nèi)的某些數(shù)據(jù),這樣的話我們可以給表單加一個(gè)submit事件,然后在事件中通過document.f.serialize()來獲取數(shù)據(jù),這種情況下代碼如下:

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