method One: Use regular expressions to get 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 used: Alert (getquerystring ("id")), then the pop-up content is 123;
Of course, if you do not pass the parameters, such as your address is abc.html after no parameters, that forced output call results sometimes error:
So we're going to add a judgment to determine whether the argument we're requesting is NULL, and first assign the value to a variable:
var myurl=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 (); Instantiation of
alert (request.id);
</script>
For example, save this code as 1.html.
So I'm going to visit 1.html?id=test.
This is the time to get the test value.
Call in HTML
<script type= "Text/javascript" >
var a= "http://baidu.com";
</script>
<body>
<a id= "A1" href= "" >sadfsdfas</a>
<script>
var A1=document.getelementbyid ("A1");
A1.href=a;
</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 what's 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.
Start
Required option. The starting position of the desired substring. The index of the first character in the string is 0.
Length
Options 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 relevant parameters are listed below:
Str.tolowercase () Convert to lowercase
Str.touppercase () strings are all converted to uppercase
URL is: Uniform Resource Locator (uniform Resource Locator, URL)
The complete URL is composed of these parts:
Scheme://host:port/path?query#fragment
Scheme: Communication protocol
Commonly used Http,ftp,maito, etc.
Host: Hosts
Server (computer) domain Name System (DNS) host name or IP address.
Port: Port number
Integer, optional, omit when using the default port of the scheme, such as HTTP's default port is 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 dynamic Web pages, such as using CGI, ISAPI, php/jsp/asp/asp. NET, and other technology-made pages, can have multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.
Fragment: Pieces of information
A string that specifies the fragment in the network resource. For example, a Web page has multiple noun interpretations, which can be directly positioned to a noun interpretation using fragment. (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
Entire URL string (in the browser is the complete address bar)
This example returns a value of: Http://www.maidq.com/index.html?ver=1.0&id=6#imhere
2,window.location.protocol
The protocol part of the URL
This example returns the value: http:
3,window.location.host
Host part of URL
This example returns a value of: www.maidq.com
4,window.location.port
Port portion of URL
If the default 80 port (update: Even if added: 80), then the return value is not the default of 80 but null characters
This example returns the value: ""
5,window.location.pathname
The path portion of the URL (that is, the file address)
This example returns a value of:/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 value of a parameter that is believed to be
This example returns a value:? ver=1.0&id=6
7,window.location.hash
Anchor Point
This example returns a value: #imhere