[to] Use JS to get the Address bar parameter method (super Simple)

Source: Internet
Author: User

This article transferred from: http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html

Method One: Use regular expressions to get the address bar parameters: (highly recommended, both practical and convenient!) )

?
function GetQueryString(name){     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");     var r = window.location.search.substr(1).match(reg);     if(r!=null)returnunescape(r[2]); return null;}// 调用方法alert(GetQueryString("参数名1"));alert(GetQueryString("参数名2"));alert(GetQueryString("参数名3"));

Here's an example:

If the Address bar URL is:abc.html?id=123&url=http://www.maidq.com

So, but you use the above method to invoke: Alert (getquerystring ("url"));

A dialog box pops up: The content is http://www.maidq.com

If using: Alert (getquerystring ("id")), then the popup content is 123;

Of course, if you do not pass parameters, such as your address is abc.html back no parameters, the force output call results sometimes error:

So we have to add a judgment to determine if the argument we requested is empty, first assign the value to a variable:

?
varmyurl=GetQueryString("url");if(myurl !=null&& myurl.toString().length>1){   alert(GetQueryString("url"));}

This will not be an error!

Method Two: Traditional methods

<script type= "Text/javascript" > Function Urlsearch () {var name,value; var str=location.href;    Get the entire address bar Var num=str.indexof ("?") Str=str.substr (num+1); Get all Parameters Stringvar. substr (start [, length ]

var arr=str.split ("&");     Each parameter is placed in the array for (Var i=0;i < arr.length;i++) {Num=arr[i].indexof ("=");      if (num>0) {name=arr[i].substring (0,num);      Value=arr[i].substr (num+1);      This[name]=value; }}} var request=new urlsearch (); Instantiate alert (request.id); </script>

For example, save this code as 1.html.

So I'm going to visit 1.html?id=test

At this point, I'm going to take the value of test.

In HTML call <script type= "Text/javascript" > var a= "http://baidu.com"; </script>

<script type= "Text/javascript" > var a= "HTTP://XXX.COM/GG.HTM?CCTV"; var s=a.indexof ("?"); var t=a.substring (s+1);//T is the thing behind it.
</script>

Stringvar.substr (start [, length ]

Returns a substring of the specified length starting at the specified position.

Stringvar

Required option. The string literal or string object to extract the substring from.

Start

Required option. The starting position of the desired substring. The index of the first character in the string is 0.

Length

Options are available. The number of characters that should be included in the returned substring.

If length is 0 or negative, an empty string is returned. If this argument is not specified, the substring continues to the end of the stringvar.

Some related parameters are listed below:

Str.tolowercase () Convert to lowercase str.touppercase () string all converted to uppercase

URL that is: Uniform Resource Locator (Uniform Resource Locator, url) The complete URL consists of these parts: Scheme://host:port/path?query#fragment Scheme: Communication protocol Common Http,ftp,maito, etc.

Host server (computer) domain Name System (DNS) hostname or IP address.

Port: The port number is an integer, optional, omitted when using the default port of the scenario, such as HTTP with the default port of 80.

Path: A string separated by 0 or more "/" symbols, typically used to represent a directory or file address on a host.

Query: Queries are optional for use with dynamic Web pages such as CGI, ISAPI, php/jsp/asp/asp. NET and other technical Web pages) pass parameters, can have multiple parameters, separated by the "&" symbol, the name and value of each parameter is separated by the "=" symbol.

Fragment: A message fragment string that specifies a fragment in a network resource. For example, there are multiple noun interpretations in a Web page, and you can use fragment to navigate directly to a noun interpretation. (also known as anchor points.)

For such a URL

Http://www.maidq.com/index.html?ver=1.0&id=6#imhere

We can use JavaScript to get every part of it.

1, window.location.href the entire URL string (in the browser is the full address bar) The return value of this example: Http://www.maidq.com/index.html?ver=1.0&id=6#imhere

The protocol part of the 2,window.location.protocol URL This example returns the value: http:

3,window.location.host the host part of the URL This example returns a value: Www.maidq.com

The port portion of the 4,window.location.port URL if the default port of 80 (update: Even if added: 80), the return value is not the default of 80 but the null character into return value: ""

5,window.location.pathname the path portion of the URL (that is, the file address) The return value of this example:/fisker/post/0703/window.location.html

6,window.location.search Query (Parameters) section in addition to the dynamic language assignment, we can also give a static page, and use JavaScript to obtain the value of the parameters believed to be the return value of this example:? ver=1.0&id=6

7,window.location.hash Anchor Point This example returns the value: #imhere

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.