The Split method was rarely used in the past. I found some materials today.
Usage: myArray = string. split ("| ")
Note: "|" indicates the feature character to be cut, string indicates the string to be cut, and myArray indicates the cut result (stored in an array). Use myArray [n] and n = myArray. length.
At first, I used the thinking in asp to think about how to get the maximum subscript of myArray. It must not be Ubound (myArray). I did not find it for a long time, and finally I got it, in fact, arrays in Javascript have a length attribute, myArray. length-1 is the maximum subscript of the myArray array. It's easy to think about it.
Let's look at an example using split: use javascript to get the address bar parameters.
Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
Function Request (strName)
{
Var strHref = "www.nextway.cn/index.htm? A = 1 & B = 1 & c = Split instance ";
Var intPos = strHref. indexOf ("? ");
Var strRight = strHref. substr (intPos + 1 );
Var arrTmp = strRight. split ("&");
For (var I = 0; I <arrTmp. length; I ++)
{
Var arrTemp = arrTmp [I]. split ("= ");
If (arrTemp [0]. toUpperCase () = strName. toUpperCase () return arrTemp [1];
}
Return "";
}
Alert (Request (""));
Alert (Request ("B "));
Alert (Request ("c "));
// -->
</Script>
There is another way to obtain the address bar parameters, the regular expression:
Copy codeThe Code is as follows:
<Script>
String. prototype. getQuery = function (name)
{
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )");
Var r = this. substr (this. indexOf ("\? ") + 1). match (reg );
If (r! = Null) return unescape (r [2]); return null;
}
Var str = "www.nextway.cn/index.htm? A = 1 & B = 1 & c = Split instance ";
Alert (str. getQuery (""));
Alert (str. getQuery ("B "));
Alert (str. getQuery ("c "));
</Script>