It is recommended to use js to Obtain Address Bar parameters (super simple) and js to obtain
Method 1:Use regular expressions to obtain the address bar parameters: (it is strongly recommended to be 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 "));
The following is an example:
If the address bar URL is: abc.html? Id = 123 & url = http://www.bkjia.com
So, but you use the following method to call: alert (GetQueryString ("url "));
A dialog box is displayed: The content is the http://www.bkjia.com
If you use: alert (GetQueryString ("id"); then the pop-up content is 123;
Of course, if you do not pass the parameter, for example, if your address is abc.html and there is no parameter after it, the following error may occur when you output the call result:
Therefore, we need to add a judgment to determine whether the request parameters are null. First, assign the value to a variable:
var myurl=GetQueryString("url"); if(myurl !=null && myurl.toString().length>1) { alert(GetQueryString("url")); }
In this way, no error is reported!
Method 2: Traditional Method
<Script type = "text/javascript"> function UrlSearch () {var name, value; var str = location. href; // obtain the entire address bar var num = str. indexOf ("? ") Str = str. substr (num + 1); // obtain all the stringvar parameters. substr (start [, length] var arr = str. split ("&"); // put each parameter 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>
Save the code as 1.html.
So I want to define 1.html? Id = test
At this time, the value of test is obtained.
Call in html
<Script type = "text/javascript"> var a = "http://baidu.com "; </script>
Stringvar. substr (start [, length]
Returns a substring of the specified length starting from the specified position.
Stringvar
Required. The String text or String object to extract the substring.
Start
Required. The starting position of the required substring. The index of the first character in the string is 0.
Length
Optional. The number of characters to be included in the returned substring.
If the length is 0 or negative, an empty string is returned. If this parameter is not specified, the substring will continue to the end of stringvar.
The following lists some related parameters:
Str. toLowerCase () 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: host
The host name or IP address of the server (Computer) Domain Name System (DNS.
Port: port Number
INTEGER (optional) Default port used when the scheme is omitted. For example, the default port of http is 80.
Path: path
A string separated by zero or multiple "/" symbols. It is generally used to represent a directory or file address on the host.
Query: query
Optional. Used for dynamic web pages (such as CGI, ISAPI, PHP/JSP/ASP. NET and other technologies to produce web pages) transmission parameters, can have multiple parameters, separated by the "&" symbol, each parameter name and value are separated by the "=" symbol.
Fragment: Information fragment
String used to specify fragments in network resources. For example, if a webpage contains multiple glossary, you can use fragment to directly locate the glossary. (Also called anchor .)
For such a URL
Http://www.bkjia.com/index.html? Ver = 1.0 & id = 6 # imhere
We can use javascript to obtain all the parts.
1, window. location. href
The whole URl string (the complete address bar in the browser)
Returned value in this example: http://www.bkjia.com/index.html? Ver = 1.0 & id = 6 # imhere
2, window. location. protocol
URL protocol section
Returned value in this example: http:
3, window. location. host
Host part of the URL
Returned value in this example: www.jb51.net
4, window. location. port
URL Port
If the default port 80 is used (update: Even if port 80 is added), the return value is not the default port 80, but an empty character.
Returned value in this example :""
5, window. location. pathname
URL path (that is, the file address)
Returned value in this example:/fisker/post/0703/window.location.html
6. window. location. search
Query (parameter)
In addition to assigning values to dynamic languages, we can also assign values to static pages and use javascript to obtain expected parameter values.
Return Value in this example :? Ver = 1.0 & id = 6
7. window. location. hash
Anchor
Returned value in this example: # imhere
The above method recommendation for getting address bar Parameters Using js (super simple) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for more.