JavaScript 页面间传值
转自:http://blog.csdn.net/qq380107165/article/details/7330612
一:JavaScript静态页面值传递之URL篇
能过URL进行传值,把要传递的信息接在URL上。
例:
- 参数传出页面Post01.html
姓名:<input type="text" name="username">
性别:<input type="text" name="sex">
<input type="button" value="传值给Read页面" onclick="Post()"> <script language="javascript" >
function Post() {
//单个值 Read.htm?username=baobao;
//多全值 Read.htm?username=baobao&sex=male;
var url = "Read.html?username="+decodeURI(document.all.username.value);
url += "&sex=" + decodeURI(document.all.sex.value);
location.href = url;
}
</script> - 参数接收页面Read01.html
方法一:
var url=location.search;
var Request = new Object();
if(url.indexOf("?")!=-1)
{
var str = url.substr(1), //去掉?号
aStrs= str.split("&");
for(var i=0;i<aStrs.length;i++)
{
Request[aStrs[i].split("=")[0]]=decodeURIComponent(aStrs[i].split("=")[1]);
}
}
alert('姓名:' + Request["username"]);
alert('性别:' + Request["sex"]);
方法二:封装为Request函数
function Request(url,strName)
{
var strHref = url;
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(decodeURIComponent(arrTemp[0]).toUpperCase() == strName.toUpperCase())
return decodeURIComponent(arrTemp[1]);
}
return "";
}
alert('姓名:' + Request(location.search,"username"));
alert('性别:' + Request(location.search,"sex"));
方法三:在String.prototype上添加方法
String.prototype.getQuery = function(name)
{
var reg = new RegExp("(^|&)"+ name+"=([^&]*)(&|$)");
var r = this.substr(this.indexOf("?")+1).match(reg);
if(r!=null) return decodeURIComponent(r[2]);
return null;
}
var str = location.search;
alert('姓名:' + str.getQuery("username"));
alert('性别:' + str.getQuery("sex"));
优点:取值方便.可以跨域.
缺点:值长度有限制
二:JavaScript静态页面值传递之Cookie篇
Cookie是浏览器存储少量命名数据,它与某个特定的网页或网站关联在一起。
Cookie用来给浏览器提供内存,以便脚本和服务器程序可以在一个页面中使用另一个页面的输入数据。
例:
- 参数传出页面Post02.html
<input type="text" name="txt1">
<input type="button" value="Post" id="btn">
<script>
function setCookie(name,value)
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date();
exp.setTime(exp.getTime() +Days*24*60*60*1000);
document.cookie = name +"="+ decodeURI(value) + ";expires=" + exp.toGMTString();
location.href = "Read02.html";//接收页面.
}
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
setCookie('mycookie',document.all.txt1.value);
}
</script>
- 参数接收页面Read02.html
function getCookie(name)
{
var arr =document.cookie.match(new RegExp("(^|)"+name+"=([^;]*)(;|$)"));
if(arr !=null) return decodeURIComponent(arr[2]); return null;
}
alert(getCookie("mycookie"));优点:可以在同源内的任意网页内访问,生命期可以设置。
缺点:值长度有限制。
三:JavaScript静态页面值传递之Window.open篇
这两窗口之间存在着关系,父窗口parent.html打开子窗口son.html。
子窗口可以通过window.opener指向父窗口,这样可以访问父窗口的对象。
例:
- 参数传出页面parent.html
<input type="text" name="maintext">
<input type="button" value="Open" id="btn">
<script>
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
// window.open(URL,name,features,replace)
// URL->新窗口地址; name->新窗口的名称; features->新窗口要显示的标准浏览器的特征;
// replace->装载到窗口的 URL 是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的当前条目
window.open('Read03.html');
}
</script>
- 参数接收页面son.html
//window.open打开的窗口.
//利用opener指向父窗口.
var parentText = window.opener.document.all.maintext.value;
alert(parentText);优点:取值方便,只要window.opener指向父窗口,就可以访问所有对象。不仅可以访问值,还可以访问父窗口的方法,值长度无限制。
缺点:两窗口要存在着关系,就是利用window.open打开的窗口,不能跨域。
JavaScript 页面间传值的更多相关文章
- JAVASCRIPT实现的WEB页面跳转以及页面间传值方法
在WEB页面中,我们实现页面跳转的方法通常是用LINK,BUTTON LINK ,IMG LINK等等,由用户点击某处,然后直接由浏览器帮我们跳转. 但有时候,需要当某事件触发时,我们先做一些操作,然 ...
- mui框架如何实现页面间传值
mui框架如何实现页面间传值 我的传值 listDetail = '<li class="mui-table-view-cell mui-media>">< ...
- 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 ...
随机推荐
- Android Studio modify language level to Java 8
If you need use lambda, should modify language level File -> Project Structure -> app -> Pr ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- 用intellij Idea加载eclipse的maven项目全流程
eclipse的maven项目目录 全流程 加载项目 打开intellij Idea file -> new -> module from existing Sources 选择.pom ...
- Mysql数据库的压力
rationalError: (2006, 'MySQL server has gone away') 2017年10月10日 20:04:43 阅读数:377 问题描述 使用django+celer ...
- ActiveMQ测试实例
ActiveMQ的安装与启动 1 下载ActiveMQ:http://activemq.apache.org/download.html 2 下载后解压到任意文件夹,解压后文件夹内的目录为: 3 进入 ...
- java堆内存模型
广泛地说,JVM堆内存被分为两部分——年轻代(Young Generation)和老年代(Old Generation). 年轻代 年轻代是所有新对象产生的地方.当年轻代内存空间被用完时,就会触发垃 ...
- jmeter插件之jsonpath提取响应结果和做断言
准备工作: 1. jmeter3.X已经自带了提取响应结果的插件:JSON Extractor 2. 下载断言插件:https://jmeter-plugins.org/wiki/JSONPathAs ...
- 常用模块(xml)
XML(可扩展性标记语言)是一种非常常用的文件类型,主要用于存储和传输数据.在编程中,对XML的操作也非常常见. 本文根据python库文档中的xml.etree.ElementTree类来进行介绍X ...
- ACM做题随做随思
程序停止运行:数组开太大: 输入一串单词,可以“string s; while(cin>>s){//代码块}”,因为cin>>s遇到空格会停止: map<key,valu ...
- JavaSE复习(二)集合
Collection List(存取有序,有索引,可以重复) ArrayList 底层是数组实现的,线程不安全,查找和修改快,增和删比较慢 LinkedList 底层是链表实现的,线程不安全,增和删比 ...