Home >Web Front-end >JS Tutorial >Example analysis of four methods of parameter transfer between html pages using javascript_javascript skills
The examples in this article describe four methods of using JavaScript to transfer parameters between HTML pages. Share it with everyone for your reference, the details are as follows:
We know that on the server side asp, jsp and other programs can accept parameters from the form on the html page. So, can I pass parameters to the html page? Can.
Principle: Obtain each parameter through the separator in window.location.href
Method 1:
/* *函数功能:从href获得参数 *sHref: http://www.cscenter.com.cn/arg.htm?arg1=d&arg2=re *sArgName:arg1, arg2 *return: the value of arg. d, re */ function GetArgsFromHref(sHref, sArgName) { var args = sHref.split("?"); var retval = ""; if(args[0] == sHref) /*参数为空*/ { return retval; /*无需做任何处理*/ } var str = args[1]; args = str.split("&"); for(var i = 0; i < args.length; i ++) { str = args[i]; var arg = str.split("="); if(arg.length <= 1) continue; if(arg[0] == sArgName) retval = arg[1]; } return retval; }
Method 2:
function getvalue(name) { var str=window.location.search; if (str.indexOf(name)!=-1) { var pos_start=str.indexOf(name)+name.length+1; var pos_end=str.indexOf("&",pos_start); if (pos_end==-1) { return str.substring(pos_start); } else { return str.substring(pos_start,pos_end) } } else { return "没有这个name值"; } } alert(getvalue(name));
Method 3:
Request = { QueryString : function(item){ var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i")); return svalue ? svalue[1] : svalue; } } alert(Request.QueryString("id"));
Method 4:
var url=location.search; var Request = new Object(); if(url.indexOf("?")!=-1) { var str = url.substr(1); //去掉?号 strs = str.toLowerCase(); strs = strs.split("&"); for(var i=0;i<strs.length;i++) { Request[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } var mapWidth = Request["w"]; var mapHeight = Request["h"];
I hope this article will be helpful to everyone in JavaScript programming.