(转)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>">< ...
随机推荐
- JavaScript高级程序设计43.pdf
事件类型 Web浏览器中有很多事件类型,“DOM3级事件”规定了以下几类事件 UI事件(用户界面),当用户与页面上的元素交互时触发: 焦点事件,当元素获得或失去焦点时触发 鼠标事件,当用户通过鼠标在页 ...
- Python运行机制
闲来无事,简单画了一下Python的运行机制,纯属娱乐:
- 2 weekend110的SecureCRTPortable远程连接 + 上传安装jdk + 上传安装配置hadoop
企业公认的最新文本版本: https://archive.apache.org/dist/ 玩玩这个远程连接软件,是个绿色软件. 别人已经做好了的. 解压之后, 下面,软件展示下, 这会 ...
- Yii框架tips
db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何在页面下边显示sql的查询时间在log组件的routes中加入 array('class' ...
- 【Caffe 测试】Training LeNet on MNIST with Caffe
Training LeNet on MNIST with Caffe We will assume that you have Caffe successfully compiled. If not, ...
- IT项目管理的六种错误思维
导读:在软件行业,在界面设计没有正式展现给客户之前,所有的工作都处于需求调研阶段.很多IT项目经理因为年轻,初生牛犊不怕虎,胆量大,勇气足,敢于在实践中引入新的工具.方法.敢于尝试不是坏事,但试验的风 ...
- xom报错 Exception in thread "main" java.net.UnknownHostException: file
Exception in thread "main" java.net.UnknownHostException: file at java.net.AbstractPlainSo ...
- 排列组合函数next_permutation()
next_permution(),按照字典序进行排列组合, 括号里的参数为类似sort里面的参数,用法相同 #include <bits/stdc++.h> using namespace ...
- 关于一次Weblogic活动线程的问题处理
Weblogic控制台监控发现 环境>>服务器>>你的服务器>>监控>>线程 中活动执行线程竟然是2000多.同一套系统在另一套平台上,并且访问的人不少 ...
- HAProxy 基本翻译
REF:http://cbonte.github.io/haproxy-dconv/1.5/configuration.html Proxy configuration can be located ...