Today encountered in a page to get another page URL passed over the parameters, at first instinctively thought of using split ("?") Such a step-by-step decomposition of the required parameters.
After thinking about it, there will certainly be a more simple way! So I found two very simple and practical ways to do this online, mark.
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) return unescape (r[2]); return null;} Call Method Alert (getquerystring ("parameter name 1")), Alert (getquerystring ("parameter Name 2"), Alert (getquerystring ("parameter name 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 whether the parameters of our request is empty, first assign the value to a variable, so that no error! :
var myurl=getquerystring ("url"), if (Myurl!=null && myurl.tostring (). length>1) { alert (getquerystring ("url"));
Method Two
functiongetrequest () {varurl = location.search;//gets the URL in the "?" String after the character varTherequest =NewObject ();if(Url.indexof ("?")! =-1) {varstr = URL.SUBSTR (1); STRs= Str.split ("&"); for(vari = 0; i < strs.length; i + +) {Therequest[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]); } } returntherequest;}//Call method var Request = new Object ();Request = Getrequest ();varparameter 1, parameter 2, parameter 3, parameter n; parameter 1= request[' parameter 1 ']; parameter 2= request[' parameter 2 ']; parameter 3= request[' Parameter 3 ']; parameter n= request[' parameter n '];
Method Three: Traditional methods
functionUrlsearch () {varName,value; varStr=location.href;//get the entire address bar varNum=str.indexof ("?")) Str=str.substr (num+1);//Get all Parameters Stringvar.substr (start [, length] varArr=str.split ("&");//each parameter is placed in the array for(varI=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; } } } //Calling Methodsvarrequest=NewUrlsearch ();//instantiation ofalert (request.id);
Some related parameters are listed below:
Str.tolowercase () Convert to lowercase
Str.touppercase () string all converted to uppercase
URL: Uniform Resource Locator (Uniform Resource Locator, URL)
The complete URL consists of the following parts:
Scheme://host:port/path?query#fragment
Scheme: Communication protocol
Common Http,ftp,maito, etc.
Host: Hosts
The server (computer) domain Name System (DNS) hostname or IP address.
Port: Port number
An integer, optionally, that omits the default port for using the scenario, such as HTTP, with the default port of 80.
Path: Paths
A string separated by 0 or more "/" symbols, typically used to represent a directory or file address on a host.
Query: Querying
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: Pieces of information
String that specifies the fragment in the 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)
This example returns the value: Http://www.maidq.com/index.html?ver=1.0&id=6#imhere
2, Window.location.protocol
The protocol portion of the URL
This example returns the value: http:
3, Window.location.host
The host part of the URL
This example returns the value: Www.maidq.com
4, Window.location.port
The port portion of the URL
If you are using the default 80 port (update: Even if you added: 80), the return value is not the default of 80 but the null character
This example returns the value: ""
5, Window.location.pathname
The path portion of the URL (that is, the file address)
This example returns the value:/fisker/post/0703/window.location.html
6, Window.location.search
Query (Parameters) Section
In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the values of the arguments we believe in.
This example returns the value:? ver=1.0&id=6
7, Window.location.hash
Anchor Point
This example returns the value: #imhere
Original address: http://www.ibloger.net/article/398.html
JavaScript three ways to get address bar parameters