JavaScript URL chapter transfer of the value of static pages

You can pass through the URL value. The information to be communicated connected to the URL.

Post.htm

ExpandedBlockStart.gif
<input type="text" name="username">
<input type="text" name="sex">
<input type="button" value="Post">
<script language="javascript" >
function Post()
{
//单个值 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

ExpandedBlockStart.gif
< Script Language = " JavaScript " > / * * ----------------- --------------- Read.htm * the Request [Key ] * function: ASP acquisition URL string, Request ( "AAA") * parameters: key, string. * examples: Alert (Request [ "AAA"]) * ----------- Request.htm ----------------- ---- * / var url = location.search; var Request  = new new  Object (); IF (url.indexOf ( " ? " ) = -! . 1 ) { var STR  =  url.substr ( . 1 ) // remove number?   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><script language="JavaScript">
<!--
function Request(strName)
{
var strHref 
= "www.abc.com/index.htm?a=1&b=1&c=测试测试";
var intPos 
= strHref.indexOf("?");
var strRight 
= strHref.substr(intPos + 1);
var arrTmp 
= strRight.split("&");
for(var i = 0; i < arrTmp.length; i++)
{
var arrTemp 
= arrTmp[i ].split("=");
if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
}
return "";
}
alert(Request(
"a"));
alert(Request(
"b"));
alert(Request(
"c"));
//-->
</script>
<script>
String.prototype.getQuery 
= function(name)
{
var reg 
= new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r 
= this.substr(this.indexOf("?")+1).match(reg);
if (r!=nullreturn unescape(r[2]); return null;
}
var str 
="www.abc.com/index.htm?a=1&b=1&c=测试测试";
alert(str.getQuery(
"a"));
alert(str.getQuery(
"b"));
alert(str.getQuery(
"c"));
</script

Pros: convenient argument can be cross-domain.
Disadvantages: the value of the length of the limited

two: JavaScript static pages Cookie articles of value transfer
. Cookie is a browser stores a small amount of named data
associated with it together with a particular page or site.
Cookie use to provide memory to the browser,
so that the script and server programs can use the input data to another page in a page.

Post.htm

ExpandedBlockStart.gif
<input type="text" name="txt1">
<input type="button" value="Post">
<script language="javascript" >
function setCookie(name,value)
{
/*
*--------------- setCookie(name,value) -----------------
* setCookie(name,value)
* 功能:设置得变量name的值
* 参数:name,字符串;value,字符串.
* 实例: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"//接收页面.
}
</script

Read.htm

ExpandedBlockStart.gif
<script language="javascript" >
function getCookie(name)
{
/*
*--------------- getCookie(name) -----------------
* getCookie(name)
* 功能:取得变量name的值
* 参数:name,字符串.
* 实例:alert(getCookie("baobao"));
*--------------- getCookie(name) -----------------
*/
var arr 
= document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr !=nullreturn unescape(arr[2]); return null;
}
alert(getCookie(
"baobao"));
</script

优点:可以在同源内的任意网页内访问.生命期可以设置.
缺点:值长度有限制.

三:JavaScript静态页面值传递之Window.open篇
这两窗口之间存在着关系.父窗口parent.htm打开子窗口son.htm
子窗口可以通过window.opener指向父窗口.这样可以访问父窗口的对象.

Post.htm

ExpandedBlockStart.gif
<input type=text name=maintext>
<input type=button value="Open">
Read.htm
<script language="javascript" >
//window.open打开的窗口.
//利用opener指向父窗口.
var parentText = window.opener.document.all.maintext.value;
alert(parentText);
</script

优点:取值方便.只要window.opener指向父窗口,就可以访问所有对象.不仅可以访问值,还可以访问父窗口的方法.值长度无限制.
缺点:两窗口要存在着关系.就是利用window.open打开的窗口.不能跨域.

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/08/11/2135008.html

Guess you like

Origin blog.csdn.net/weixin_34168700/article/details/93494956
Recommended