1、页面数据传递需要引入一个js文件,代码
UrlParm = function() { // url参数 var data, index; (function init() { data = []; index = {}; var u = window.location.search.substr(1); if (u != ‘‘) { var parms = decodeURIComponent(u).split(‘&‘); for (var i = 0, len = parms.length; i < len; i++) { if (parms[i] != ‘‘) { var p = parms[i].split("="); if (p.length == 1 || (p.length == 2 && p[1] == ‘‘)) {// p | p= data.push([‘‘]); index[p[0]] = data.length - 1; } else if (typeof(p[0]) == ‘undefined‘ || p[0] == ‘‘) { // =c | = data[0] = [p[1]]; } else if (typeof(index[p[0]]) == ‘undefined‘) { // c=aaa data.push([p[1]]); index[p[0]] = data.length - 1; } else {// c=aaa data[index[p[0]]].push(p[1]); } } } } })(); return { // 获得参数,类似request.getParameter() parm : function(o) { // o: 参数名或者参数次序 try { return (typeof(o) == ‘number‘ ? data[o][0] : data[index[o]][0]); } catch (e) { } }, //获得参数组, 类似request.getParameterValues() parmValues : function(o) { // o: 参数名或者参数次序 try { return (typeof(o) == ‘number‘ ? data[o] : data[index[o]]); } catch (e) {} }, //是否含有parmName参数 hasParm : function(parmName) { return typeof(parmName) == ‘string‘ ? typeof(index[parmName]) != ‘undefined‘ : false; }, // 获得参数Map ,类似request.getParameterMap() parmMap : function() { var map = {}; try { for (var p in index) { map[p] = data[index[p]]; } } catch (e) {} return map; } } }();
2、页面1
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>测试页面</title> </head> <body> <a href="test1.html?name=张三&pass=123123">test1.html</a> </body> </html>
3、页面2
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="js/getUrlParam.js"></script> <script> $(function(){ var name = UrlParm.parm("name"); var pass = UrlParm.parm("pass"); alert(name+"!"+pass); //页面跳转之后就可以获取数据 }) </script> </head> <body> 这是test1页面 </body> </html>
简单测试一下,完全正确,感谢愤怒的火柴写的js代码!!!!
原文链接:https://blog.csdn.net/caoyuan10036/article/details/7227214
原文地址:https://www.cnblogs.com/newbest/p/9414211.html
时间: 2024-10-12 17:45:23