- 情景描述
目前问题是这样的:博客列表中罗列了博客的大致信息,现在我要查看博客的详细内容,我需要跳转到一个博客显示页面,按照前后端分离的设想,所有的页面都是静态页面,那么,(1)每个博客都是一个独立的静态页面,每写一篇博客就建立对应的一个静态页面,所有静态页面之间不需要传值,(2)只写一个博客展示页面,通过该页面自身发送ajax请求,改变显示的博客内容,但是,需要管理页面传值告诉展示页面该请求哪篇博客的内容进行展示。
所以,我们的问题是,怎样实现静态页面之间的传值?
- 思路
1、url传值
1、A页面跳转B页面并传参
location.href = "B.htm?username= xxx";
2、B页面解析url中的参数
var url=location.search;
var request = new Object();
if(url.indexOf("?")!=-1)
{
var str = url.substr(1) //去掉?号
strs = str.split("&");
for(var i=0;i<strs.length;i++)
{
request[strs[i ].split("=")[0]] = strs[ i].split("=")[1];
}
}
2、cookie传值
1、A页面将数据存入cookie并跳转到B页面
document.cookie = name + "="+ value + ";expires=" + time;
location.href = "B.html"; //接收页面
2、B页面读取cookie中的数据
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
3、Window.open
1、A页面利用window.open打开B页面
window.open(B.html);
2、B页面利用opener指向父窗口A,获取其中的数据
var parentText = window.opener.document.all.maintext.value;
参考:https://www.cnblogs.com/mingjian/p/4166782.html