靜態(tài)頁(yè)面的值傳遞(三部曲)
更新時(shí)間:2006年09月25日 00:00:00 作者:
這兩窗口之間存在著關(guān)系.父窗口parent.htm打開(kāi)子窗口son.htm
子窗口可以通過(guò)window.opener指向父窗口.這樣可以訪問(wèn)父窗口的對(duì)象.
優(yōu)點(diǎn):取值方便.只要window.opener指向父窗口,就可以訪問(wèn)所有對(duì)象.
不僅可以訪問(wèn)值,還可以訪問(wèn)父窗口的方法.值長(zhǎng)度無(wú)限制.
缺點(diǎn):兩窗口要存在著關(guān)系.就是利用window.open打開(kāi)的窗口.不能跨域.
Post.htm
<input type=text name=maintext>
<input type=button onclick="window.open('Read.htm')" value="Open">
Read.htm
<script language="javascript" >
//window.open打開(kāi)的窗口.
//利用opener指向父窗口.
var parentText = window.opener.document.all.maintext.value;
alert(parentText);
</script>
利用Cookie.
Cookie是瀏覽器存儲(chǔ)少量命名數(shù)據(jù).
它與某個(gè)特定的網(wǎng)頁(yè)或網(wǎng)站關(guān)聯(lián)在一起.
Cookie用來(lái)給瀏覽器提供內(nèi)存,
以便腳本和服務(wù)器程序可以在一個(gè)頁(yè)面中使用另一個(gè)頁(yè)面的輸入數(shù)據(jù).
優(yōu)點(diǎn):可以在同源內(nèi)的任意網(wǎng)頁(yè)內(nèi)訪問(wèn).生命期可以設(shè)置.
缺點(diǎn):值長(zhǎng)度有限制.
Post.htm
<input type="text" name="txt1">
<input type="button" onclick="setCookie('baobao',document.all.txt1.value)" value="Post">
<script language="javascript" >
function setCookie(name,value)
{
/*
*--------------- setCookie(name,value) -----------------
* setCookie(name,value)
* 功能:設(shè)置得變量name的值
* 參數(shù):name,字符串;value,字符串.
* 實(shí)例:setCookie('username','baobao')
*--------------- setCookie(name,value) -----------------
*/
var Days = 30; //此 cookie 將被保存 30 天
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
location.href = "Read.htm"; //接收頁(yè)面.
}
</script>
Read.htm
<script language="javascript" >
function getCookie(name)
{
/*
*--------------- getCookie(name) -----------------
* getCookie(name)
* 功能:取得變量name的值
* 參數(shù):name,字符串.
* 實(shí)例:alert(getCookie("baobao"));
*--------------- getCookie(name) -----------------
*/
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=null) return unescape(arr[2]); return null;
}
alert(getCookie("baobao"));
</script>
URL篇
能過(guò)URL進(jìn)行傳值.把要傳遞的信息接在URL上.
優(yōu)點(diǎn):取值方便.可以跨域.
缺點(diǎn):值長(zhǎng)度有限制.
Post.htm
<input type="text" name="username">
<input type="text" name="sex">
<input type="button" onclick="Post()" value="Post">
<script language="javascript" >
function Post()
{
//單個(gè)值 Read.htm?username=baobao;
//多全值 Read.htm?username=baobao&sex=male;
url = "Read.htm?username="+escape(document.all.username.value);
url += "&sex=" + escape(document.all.sex.value);
location.href=url;
}
</script>
Read.htm
<script language="javascript" >
/*
*--------------- Read.htm -----------------
* Request[key]
* 功能:實(shí)現(xiàn)ASP的取得URL字符串,Request("AAA")
* 參數(shù):key,字符串.
* 實(shí)例:alert(Request["AAA"])
*--------------- Request.htm -----------------
*/
var url=location.search;
var Request = new Object();
if(url.indexOf("?")!=-1)
{
var str = url.substr(1) //去掉?號(hào)
strs = str.split("&");
for(var i=0;i<strs.length;i++)
{
Request[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
alert(Request["username"])
alert(Request["sex"])
</script>
子窗口可以通過(guò)window.opener指向父窗口.這樣可以訪問(wèn)父窗口的對(duì)象.
優(yōu)點(diǎn):取值方便.只要window.opener指向父窗口,就可以訪問(wèn)所有對(duì)象.
不僅可以訪問(wèn)值,還可以訪問(wèn)父窗口的方法.值長(zhǎng)度無(wú)限制.
缺點(diǎn):兩窗口要存在著關(guān)系.就是利用window.open打開(kāi)的窗口.不能跨域.
Post.htm
<input type=text name=maintext>
<input type=button onclick="window.open('Read.htm')" value="Open">
Read.htm
<script language="javascript" >
//window.open打開(kāi)的窗口.
//利用opener指向父窗口.
var parentText = window.opener.document.all.maintext.value;
alert(parentText);
</script>
利用Cookie.
Cookie是瀏覽器存儲(chǔ)少量命名數(shù)據(jù).
它與某個(gè)特定的網(wǎng)頁(yè)或網(wǎng)站關(guān)聯(lián)在一起.
Cookie用來(lái)給瀏覽器提供內(nèi)存,
以便腳本和服務(wù)器程序可以在一個(gè)頁(yè)面中使用另一個(gè)頁(yè)面的輸入數(shù)據(jù).
優(yōu)點(diǎn):可以在同源內(nèi)的任意網(wǎng)頁(yè)內(nèi)訪問(wèn).生命期可以設(shè)置.
缺點(diǎn):值長(zhǎng)度有限制.
Post.htm
<input type="text" name="txt1">
<input type="button" onclick="setCookie('baobao',document.all.txt1.value)" value="Post">
<script language="javascript" >
function setCookie(name,value)
{
/*
*--------------- setCookie(name,value) -----------------
* setCookie(name,value)
* 功能:設(shè)置得變量name的值
* 參數(shù):name,字符串;value,字符串.
* 實(shí)例:setCookie('username','baobao')
*--------------- setCookie(name,value) -----------------
*/
var Days = 30; //此 cookie 將被保存 30 天
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
location.href = "Read.htm"; //接收頁(yè)面.
}
</script>
Read.htm
<script language="javascript" >
function getCookie(name)
{
/*
*--------------- getCookie(name) -----------------
* getCookie(name)
* 功能:取得變量name的值
* 參數(shù):name,字符串.
* 實(shí)例:alert(getCookie("baobao"));
*--------------- getCookie(name) -----------------
*/
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=null) return unescape(arr[2]); return null;
}
alert(getCookie("baobao"));
</script>
URL篇
能過(guò)URL進(jìn)行傳值.把要傳遞的信息接在URL上.
優(yōu)點(diǎn):取值方便.可以跨域.
缺點(diǎn):值長(zhǎng)度有限制.
Post.htm
<input type="text" name="username">
<input type="text" name="sex">
<input type="button" onclick="Post()" value="Post">
<script language="javascript" >
function Post()
{
//單個(gè)值 Read.htm?username=baobao;
//多全值 Read.htm?username=baobao&sex=male;
url = "Read.htm?username="+escape(document.all.username.value);
url += "&sex=" + escape(document.all.sex.value);
location.href=url;
}
</script>
Read.htm
<script language="javascript" >
/*
*--------------- Read.htm -----------------
* Request[key]
* 功能:實(shí)現(xiàn)ASP的取得URL字符串,Request("AAA")
* 參數(shù):key,字符串.
* 實(shí)例:alert(Request["AAA"])
*--------------- Request.htm -----------------
*/
var url=location.search;
var Request = new Object();
if(url.indexOf("?")!=-1)
{
var str = url.substr(1) //去掉?號(hào)
strs = str.split("&");
for(var i=0;i<strs.length;i++)
{
Request[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
alert(Request["username"])
alert(Request["sex"])
</script>
相關(guān)文章
基于layui內(nèi)置模塊(element常用元素的操作)
今天小編就為大家分享一篇基于layui內(nèi)置模塊(element常用元素的操作),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09JavaScript實(shí)現(xiàn)頁(yè)面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)頁(yè)面動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03javascript中函數(shù)作為參數(shù)調(diào)用的方法
這篇文章主要介紹了javascript中函數(shù)作為參數(shù)調(diào)用的方法,實(shí)例分析了函數(shù)作為操作調(diào)用的原理與相關(guān)技巧,需要的朋友可以參考下2015-02-02window.location.href IE下跳轉(zhuǎn)失效的解決方法
這篇文章主要介紹了window.location.href IE下跳轉(zhuǎn)失效的解決方法,需要的朋友可以參考下2014-03-03Markdown+Bootstrap圖片自適應(yīng)屬性詳解
這篇文章主要為大家詳細(xì)介紹了Markdown+Bootstrap圖片自適應(yīng)屬性,感興趣的朋友可以參考一下2016-05-05