(转)JS页面间传值
一:JavaScript静态页面值传递之URL篇
能过URL进行传值.把要传递的信息接在URL上.
例子:
参数传出页面Post.htm—>
<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—>
<script language="javascript" >
/*
*--------------- Read.htm -----------------
* Request[key]
* 功能:实现ASP的取得URL字符串,Request("AAA")
* 参数:key,字符串.
* 实例:alert(Request["AAA"])
*--------------- Request.htm -----------------
*/
var url=location.search;
var Request = new Object();
if(url.indexOf("?")!=-1)
{
varstr = url.substr(1) //去掉?号
strs= str.split("&");
for(vari=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)
{
varreg = new RegExp("(^|&)"+ name+"=([^&]*)(&|$)");
varr = this.substr(this.indexOf("?")+1).match(reg);
if(r!=null) return 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>
优点:取值方便.可以跨域.
缺点:值长度有限制
二:JavaScript静态页面值传递之Cookie篇
Cookie是浏览器存储少量命名数据.
它与某个特定的网页或网站关联在一起.
Cookie用来给浏览器提供内存,
以便脚本和服务器程序可以在一个页面中使用另一个页面的输入数据.
参数传出页面Post.htm—>
<inputtype="text" name="txt1">
<inputtype="button" value="Post">
<scriptlanguage="javascript" >
functionsetCookie(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—>
<scriptlanguage="javascript" >
functiongetCookie(name)
{
/*
*---------------getCookie(name) -----------------
*getCookie(name)
* 功能:取得变量name的值
* 参数:name,字符串.
* 实例:alert(getCookie("baobao"));
*---------------getCookie(name) -----------------
*/
var arr =document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));
if(arr !=null) returnunescape(arr[2]); return null;
}
alert(getCookie("baobao"));
</script>
优点:可以在同源内的任意网页内访问.生命期可以设置.
缺点:值长度有限制.
三:JavaScript静态页面值传递之Window.open篇
这两窗口之间存在着关系.父窗口parent.htm打开子窗口son.htm
子窗口可以通过window.opener指向父窗口.这样可以访问父窗口的对象.
<inputtype=text name=maintext>
<inputtype=button value="Open">
Read.htm
<scriptlanguage="javascript" >
//window.open打开的窗口.
//利用opener指向父窗口.
varparentText = window.opener.document.all.maintext.value;
alert(parentText);
</script>
优点:取值方便.只要window.opener指向父窗口,就可以访问所有对象.不仅可以访问值,还可以访问父窗口的方法.值长度无限制.
缺点:两窗口要存在着关系.就是利用window.open打开的窗口.不能跨域.
原文地址:http://blog.csdn.net/qq380107165/article/details/7330612
(转)JS页面间传值的更多相关文章
- JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="tex ...
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- iOS 页面间传值 之 属性传值,代理传值
手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- mui框架如何实现页面间传值
mui框架如何实现页面间传值 我的传值 listDetail = '<li class="mui-table-view-cell mui-media>">< ...
随机推荐
- HW4.27
public class Solution { public static void main(String[] args) { int count = 0; for(int i = 2001; i ...
- office文件在线预览,模仿网易邮箱在线预览的
最近研究了半天,代码是倾情奉送啊,C#,asp.net的 这个原理是office文件转换为PDF文件,然后再转换成SWF文件,FlexPaper+swfTools. 有个问题,需要在web.confi ...
- .Net 笔记(二) 泛型和集合
前言: 本文中介绍 泛型和集合的区别.也算是自己的一个知识点的回顾,并且把它们写在自己的笔记中. 1.集合: 在讲到集合之前,我们先来回顾下数组的知识点吧,因为集合和数组的关系也是比较微妙的各有利弊, ...
- SSH-KeyGen 的用法 【转载】
SSH-KeyGen 的用法 secureCrt通过密钥登录 做法:1.登录A机器 2.ssh-keygen -t [rsa|dsa],将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或 ...
- VS扩展CodeMaid代码整理插件
本文章转载:http://www.cnblogs.com/wintersun/p/3577039.html 官方地址:http://www.codemaid.net/ 开源VS扩展CodeMaid介绍 ...
- 基于特定领域国土GIS应用框架设计及应用
基于特定领域国土GIS应用框架 设计及应用 何仕国 2012年8月16日 摘要: 本文首先讲述了什么是框架和特定领域框架,以及与国土GIS 这个特定领 ...
- [TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS
TypeScript outputs JavaScript, but what are you supposed to do with it? This lesson shows how to tak ...
- Maven tomcat插件配置和使用
pom.xml组态 <build> <plugins> <plugin> <groupId>org ...
- ExecutorService与Executors例子的简单剖析(转)
对于多线程有了一点了解之后,那么来看看java.lang.concurrent包下面的一些东西.在此之前,我们运行一个线程都是显式调用了 Thread的start()方法.我们用concurrent下 ...
- LabVIEW设计模式系列——状态机
标准:1.状态用枚举自定义类型,便于统一管理修改.2.一般地应该有:Initialize,Idle,Stop,Blank状态.3.Initialize进行一些初始化的操作:Idle一种过渡状态,用于和 ...