用sessionStorage实现页面之间的数据传输


1、sessionStorage主要含几种方法:
  //页面A:存放一个简单的字符串
  sessionStorage.obj = '123';
  //页面B:取到给obj
  var str = sessionStorage.obj;
  //类型地:
  sessionStorage.setItem(key,value);
  sessionStorage.gettItem(key,value);
  sessionStorage.remove(key);
2、对于常用的字段传输,是没问题的,但是对于以下情况:
  //存放对象、数组
  var obj = { name:'Jim' };
  sessionStorage.obj = obj;
  localStorage.obj = obj;

  var arr = [1,2,3];
  sessionStorage.obj = arr;
  localStorage.obj = arr;
  //读取是不行的,这里应该在存放对象和数组之前,通过JSON对象提供的parse和stringify将其他数据类型转化成字符串,再存储到storage中。
  例如:
  var str = JSON.stringify(vim.todos[index]);
  //存入
  sessionStorage.setItem('newsObject',str);
  //存入记录当前页面,以便从详情页面返回时使用
  sessionStorage.setItem('currentPage',currentPage);
  sessionStorage.setItem('currentPage2',currentPage2);
  //读取
  var newsObject = sessionStorage.getItem('newsObject');
  //重新转换为对象
  newsObject = JSON.parse(newsObject);
  alert(newsObject.title);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM