您的位置:首页 > Web前端 > JavaScript

用javascript获取URL参数

2008-12-29 19:33 411 查看
/*
 * 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 like 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.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)   返回字符串,包括from不包括to,看上去有点不合情理,但请注意,这样返回字符串的长度总是等于to减去from。
如果两个参数相等,返回空串;如果前大后小,则先对调再截取;不支持负数,这一点与slice()和()不同。
只有一个参数,应该是从from到串尾。

原文:


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

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

It is important to remember that the
character at position from is included in the substring but
that the character at position to is not included in the
substring. While this may seem arbitrary or counterintuitive, a notable feature
of this system is that the length of the returned substring is always equal to to-from.

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

 

2、String.split(delimiter,
limit)    返回字符串数组,参数一为分隔符,参数二为返回字符串的长度限制,可选。

如果分隔符在一开始,则首先返回一个空串;在结尾类似。

如果不指定分隔符,则返回原句。

如果分隔符为空,返回每一个字符,这样数组长度与字符串长度相等。

 

返回的字符串一般不包含分隔符,但有特例如下:if delimiter is a regular expression that contains parenthesized subexpressions,
the substrings that match those parenthesized subexpressions (but not the text
that matches the regular expression as a whole) are included in the returned
array.

 

String.split( )与Array.join( )功能正相反。

 

原文:

The split( ) method creates and returns an
array of as many as limit substrings of the specified string. These substrings
are created by searching the string from start to end for text that matches delimiter
and breaking the string before and after that matching text. The delimiting
text is not included in any of the returned substrings, except as noted at the
end of this section. Note that if the delimiter matches the beginning of the string,
the first element of the returned array will be an empty stringthe 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) will
be the empty string.

If no delimiter is specified, the string is
not split at all, and the returned array contains only a single, unbroken string
element. If delimiter is the empty string or a regular expression that 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 that this is a special case because the empty strings before
the first character and after the last character are not matched.)

As noted earlier, the substri
4000
ngs in the array
returned by this method do not contain the delimiting text used to split the string.
However, if delimiter is a regular expression that contains parenthesized
subexpressions, the substrings that match those parenthesized subexpressions
(but not the text that matches the regular expression as a whole) are included
in the returned array.

Note that the String.split( ) method is the
inverse of the Array.join( ) method.

 

示例:

The split( ) method is most useful when you
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 is
to parse commands and similar strings by breaking them down into words
delimited 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, use the empty string as the delimiter. Use the limit argument if
you only 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 want the delimiters or one or more
portions of the delimiter included in the returned array, use a regular
expression 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 an encoded copy of its s argument. ASCII letters and digits are
not encoded, nor are the following ASCII punctuation characters:

- _ . ! ~ * ' ( )

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

Note the difference between encodeURIComponent(
) and encodeURI( ): encodeURIComponent( ) assumes that its argument is a
portion (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.

示例:encodeURIComponent("hello
world?"); // Returns hello%20world%3F

 

 

 

          

   


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息