在静态页面html中跳转传值

在html中通过"?"传值--------<a href="index2.html?name=caoy">静态传值</a>

在跳转到的页面index2.html中接收----var name=UrlParm.parm("name");

代码如下:


index.html:

  1. <script type="text/javascript"  src="js/getUrlParam.js"></script>  
  2. <a href="index2.html?name=caoy">静态传值</a>  


index2.html:

  1. <script type="text/javascript">  
  2.     var name=UrlParm.parm("name");  
  3.     alert(name);  
  4. </script>  

getUrlParam.js:

  1. UrlParm = function() { // url参数    
  2.   var data, index;    
  3.   (function init() {    
  4.     data = [];    
  5.     index = {};    
  6.     var u = window.location.search.substr(1);    
  7.     if (u != '') {    
  8.       var parms = decodeURIComponent(u).split('&');    
  9.       for (var i = 0len = parms.length; i < len; i++) {    
  10.         if (parms[i] != '') {    
  11.           var p = parms[i].split("=");    
  12.           if (p.length == 1 || (p.length == 2 && p[1] == '')) {// p | p=    
  13.             data.push(['']);    
  14.             index[p[0]] = data.length - 1;    
  15.           } else if (typeof(p[0]) == 'undefined' || p[0] == '') { // =c | =    
  16.             data[0] = [p[1]];    
  17.           } else if (typeof(index[p[0]]) == 'undefined') { // c=aaa    
  18.             data.push([p[1]]);    
  19.             index[p[0]] = data.length - 1;    
  20.           } else {// c=aaa    
  21.             data[index[p[0]]].push(p[1]);    
  22.           }    
  23.         }    
  24.       }    
  25.     }    
  26.   })();    
  27.   return {    
  28.     // 获得参数,类似request.getParameter()    
  29.     parm : function(o) { // o: 参数名或者参数次序    
  30.       try {    
  31.         return (typeof(o) == 'number' ? data[o][0] : data[index[o]][0]);    
  32.       } catch (e) {    
  33.       }    
  34.     },    
  35.     //获得参数组, 类似request.getParameterValues()    
  36.     parmValues : function(o) { //  o: 参数名或者参数次序    
  37.       try {    
  38.         return (typeof(o) == 'number' ? data[o] : data[index[o]]);    
  39.       } catch (e) {}    
  40.     },    
  41.     //是否含有parmName参数    
  42.     hasParm : function(parmName) {    
  43.       return typeof(parmName) == 'string' ? typeof(index[parmName]) != 'undefined' : false;    
  44.     },    
  45.     // 获得参数Map ,类似request.getParameterMap()    
  46.     parmMap : function() {    
  47.       var map = {};    
  48.       try {    
  49.         for (var p in index) {  map[p] = data[index[p]];  }    
  50.       } catch (e) {}    
  51.       return map;    
  52.     }    
  53.   }    
  54. }();    


这样就能通过html跳转传值了

原文地址:https://www.cnblogs.com/interdrp/p/2942556.html