This article mainly introduces four methods for passing parameters between html pages by using javascript, and analyzes common techniques for passing parameters between pages by using examples in detail, for more information about how to use javascript to transfer parameters between html pages, see the following example. We will share this with you for your reference. The details are as follows:
We know that on the server side, asp, jsp, and other programs can accept parameters sent from form on the html page. So, can we pass parameters to the html page. Yes.
Principle: parameters are obtained using the delimiter in window. location. href.
Method 1:
/** Function: Get the parameter * sHref: http://www.cscenter.com.cn/arg.htm? From href? 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)/* parameter is blank */{return retval; /* No processing required */} 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 "without this name value" ;}} 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); // remove? Strs = str. toLowerCase (); strs = strs. split ("&"); for (var I = 0; I
I hope this article will help you design JavaScript programs.