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打开的窗口.不能跨域.
JS页面间传值的更多相关文章
- (转)JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="text& ...
- 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>">< ...
随机推荐
- Ogre1.8地形和天空盒的建立(一块地形)
转自:http://www.cnblogs.com/WindyMax/ 研究Ogre的程序笔记 编译环境 WIN7 32 VS2008 Ogre的版本 1.8 Ogre的地形算法是采用Geome ...
- Python中的闭包
简单的闭包的栗子: def counter(statr_at = 0): count = 1 def incr(): nonlocal count #注意由于count类型为immutable,所以需 ...
- win下隐藏任务栏
C# 隐藏任务栏开始按钮 关闭shell 分类: .NET C# 2011-07-14 15:26 789人阅读 评论(1) 收藏 举报 shell任务c#stringnulluser 一.隐藏任务栏 ...
- Ubuntu下Eclipse中文乱码问题解决(转)
Ubuntu下Eclipse中文乱码问题解决 把Windows下的工程导入到了Linux下Eclipse中,由于以前的工程代码,都是GBK编码的(Windows下的Eclipse 默认会去读取系统的编 ...
- javascript js写特效日历
<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException
解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...
- Objective-C----MRC内存管理 、 自动释放池 、 面向对象三大特性及封装 、 继承 、 组合与聚合
1 MRC练习 1.1 问题 引用计数是Objective-C语言采用的一种内存管理技术,当一个对象被创建在堆上后,该对象的引用计数就自动设置为1,如果在其它对象中的对象成员需要持有这个对象时,则该对 ...
- busybox reboot 无效
/*********************************************************************** * busybox reboot 无效 * 说明: * ...
- OpenMP的简单使用教程
转自:http://binglispace.com/2015/01/09/openmp-intro/ OpenMP的简单使用教程 今天有幸参加了一个XSEDE OpenMP的workshop讲座,真是 ...
- 阅读学术论文的心得体会from小木虫
我们搞科研的很重要的一个环节就是文献的阅读!关于如何阅读文献?读什么,怎么读?结合我自己的体会,我想这里的关键在于要让我们通过这种方式的学习,学会看懂作者的思想.思路和科学方法,从中学习论文作者发现问 ...