Get URL parameters with JavaScript

Source: Internet
Author: User
/*
* This function parses ampersand-separated name=value argument pairs from
* The query string of the URL. It stores the Name=value pairs in
* Properties of an object and returns that object. Use it as this:
*
* var args = Getargs (); Parse args from URL
* var q = args.q | |  ""; Use argument, if defined, or a default value
* var n = args.n? parseint (ARGS.N): 10;
*/
function Getargs () {
var args = new Object ();
var query = location.search.substring (1); Get query string
var pairs = Query.split ("&"); Break at Ampersand
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexof (' = '); Look for "Name=value"
if (pos = = 1) continue; If not found, skip
var argname = pairs[i].substring (0,pos); Extract the name
var value = pairs[i].substring (pos+1); Extract the value
Value = decodeuricomponent (value); Decode it, if needed
Args[argname] = value; Store as a property
}
return args; Return the object
}


1, string.substring (from,to) return string, including from does not include to, seems a bit unreasonable, but note that this return string length is always equal to minus from.
If the two arguments are equal, return the empty string, and if the front is small, the first is reversed and then intercepted; negative numbers are not supported, unlike slice () and ().
There is only one argument, which should be from the from to the end of the string.

Original:

String.substring () returns a substring of string consisting of the characters between positions from and to. The character at position, included, but the character in position to are not included.

If from equals to, this is returns an empty (length 0) string. If greater than to, this method the two arguments and then returns the substring between.

It is important to remember this character at position from are included in the substring but this character at Pos Ition to isn't included in the substring. While this may seem arbitrary or counterintuitive, a notable feature the "This" is the length of the returned sub The string is always equal to To-from.

Note this string.slice () and the nonstandard string.substr () can also extract substrings from a String. Unlike those methods, string.substring () does not accept negative.

2, String.Split (delimiter, limit) returns an array of strings, parameter one is a separator, parameter two is the length limit of the return string, optional.

If the separator is at the beginning, an empty string is returned first, similar at the end.

If no separator is specified, the original sentence is returned.

If the delimiter is empty, returns each character so that the length of the array is equal to the length of the string.

The returned string does not normally contain delimiters, but the exception is as follows: If delimiter is a regular expression that contains parenthesized subexpressions, the substrings that Match those parenthesized subexpressions (but not the text this matches the regular expression as a whole) are included I n the returned array.

String.Split () is the opposite of the Array.join () function.

Original:

The split () method creates and returns an array of as many as limit substrings of the specified string. These are substrings are created by searching the "string from" to "to" Ring before and matching text. The delimiting text is isn't included in any of the returned substrings, except as noted at the "this" section. Note that if the delimiter matches the beginning of the string, the the ' the ' returned array would be a empty s Tringthe text that appears before the delimiter. Similarly, if the delimiter matches the end of the string, the last element of the array (assuming no conflicting limit) W Ill be the empty string.

If No delimiter is specified, the string isn't split at all, and the returned array contains only a single, unbroken stri ng element. If delimiter is the empty string or a regular expression this matches the empty string, the string is broken between each character, and the returned array has the same length as the string does, assuming no smaller limit is specified. (note, this is a special case because the empty strings before the "the", "the last character character no T matched.)

As noted earlier, the substrings in the array returned by this method does not contain the delimiting text used to split the String. However, if delimiter is a regular expression this contains parenthesized, the subexpressions that match substrings Parenthesized subexpressions (but not the text this matches the regular expression as a whole) are included in the Returne D Array.

The String.Split () is the inverse to the Array.join () method.

Example:

The split () method was most useful when your are working with highly structured strings. For example:

"1:2:3:4:5". Split (":"); Returns ["1", "2", "3", "4", "5"]
"|a|b|c|".    Split ("|"); Returns ["", "a", "B", "C", ""]

Another common use of the split () method was to parse commands and similar strings from breaking them down into words delimi Ted by Spaces:

var words = Sentence.split (');

It is easier to split a string into words using a regular expression as a delimiter:

var words = Sentence.split (//s+/);

To split a string into an array of characters and use the empty string as the delimiter. Use the ' limit argument if you are want to split a prefix of the ' string into ' an array of characters:

"Hello". Split (""); Returns ["H", "E", "L", "L", "O"]
"Hello". Split ("", 3); Returns ["H", "E", "L"]

If you are want the delimiters or one or more portions of the delimiter included in the returned array, use a regular Expressi On with parenthesized subexpressions. For example, the following code breaks a string at HTML tags and includes those tags in the returned array:

var text = "Hello <b>world</b>";

Text.split (/(<[^>]*>)/); Returns ["Hello", "<b>", "World", "</b>", ""]

3, decodeURIComponent (s)

encodeURIComponent () is a global function that returns the encoded copy of its S argument. ASCII letters and digits are not encoded, nor are the following ASCII punctuation:

- _ . ! ~ * ' ( )

All other characters, including punctuation characters such as/,:, and # This serve to separate the various components O f a URI, are replaced with one or more hexadecimal escape sequences. encodeURI () for a description of the encoding scheme used.

Note the difference between encodeuricomponent () and encodeURI (): encodeURIComponent () assumes the It argument is a P Ortion (such as the Protocol, hostname, path, or query string) of a URI. Therefore it escapes the punctuation characters that are used to separate the portions of a URI.

Example: encodeURIComponent ("Hello world?");//Returns hello%20world%3f

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.