Slice function usage instance analysis in javaScript and slice instance analysis
This article describes the slice function usage in javaScript. Share it with you for your reference. The specific analysis is as follows:
The slice function in javaScript returns an array segment for the slice function of the array object. (Still an array)
ArrayObj. slice (start, [end])
Parameters:
ArrayObj, required. An Array object.
Start, required. The starting element of the part specified in arrayObj is the subscript calculated from scratch.
End, optional. The ending element of the part specified in arrayObj is the subscript calculated from scratch.
Note:
The slice Method returns an Array object, which contains the specified part of arrayObj.
The slice Method is always copied to the end specified element, but this element is not included. If start is negative, it is processed as length + start. Here, length is the length of the array. If end is negative, it is processed as length + end. Here, length is the length of the array. If end is omitted, the slice method will always be copied to the end of arrayObj. If end appears before start, no elements are copied to the new array.
Example:
In the following example, all elements in myArray are copied to newArray except the last element:
NewArray = myArray. slice (0,-1) -------- str's slice
<Script type = "text/javascript"> function Request (valuename, testurl) {var rtnval; // obtain the current webpage address. http: // 192.168.1.240: 85/test/asp/Crmkorea_co_kr/test.htm? PARA1 = ATEST // var nowAddress = unescape (location. href); var nowAddress = testurlvar parameters = new Array (); alert (nowAddress. slice (nowAddress. indexOf ("? ") + 1, nowAddress. length) parameters = (nowAddress. slice (nowAddress. indexOf ("? ") + 1, nowAddress. length )). split ("&"); for (var I = 0; I <parameters. length; I ++) {alert (I + "--" + parameters [I]) if (parameters [I]. indexOf (valuename )! =-1) {rtnval = parameters [I]. split ("=") [1]; if (rtnval = undefined | rtnval = null) {rtnval = "" ;}return rtnval ;} else {// alert (parameters [0]. indexOf (valuename) // alert ("request must from:" + valuename)} return "" // alert (rtnval);} var myaddr = "http://www.yoursiteweb.com /? Para1 = test1 & PARA1 = test2 "alert (Request (" PARA1 ", myaddr) // check whether the address contains the para1 parameter and return the value of this parameter </script>
I hope this article will help you design javascript programs.