There are two ways to obtain address bar parameters in js: the first is to use regular expressions to obtain address bar parameters, and the second is a relatively traditional method. Here, the editor strongly recommends the use of the first method, which is convenient and practical. Please see the details below for the specific implementation process.
Method 1: Use regular expressions to obtain 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 is an example:
If the URL of the address bar is: abc.html?id=123&url=http://www.maidq.com
Then, but you use the above method to call: alert(GetQueryString("url"));
A dialog box will pop up: the content is http://www.maidq.com
If you use: alert(GetQueryString("id")); then the pop-up content is 123;
Of course, if you do not pass parameters, for example, if your address is abc.html and there is no parameter after it, then if the forced output call result will sometimes report an error:
So we need to add a judgment to determine whether the parameter we request is empty, and first assign the value to a variable:
var myurl=GetQueryString("url");if(myurl !=null && myurl.toString().length>1){alert(GetQueryString("url"));}This way you won’t report an error!
Method 2: Traditional Method
<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("&"); //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>For example, save this code as 1.html
Then I want to access 1.html?id=test
At this time, the test value is obtained
Called in html
<script type="text/javascript">var a="http://baidu.com";</script></head><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 comes after it</script>stringvar.substr(start [, length ]
Returns a substring of the specified length starting from the specified position.
stringvar
Required option. A string literal or String object to extract a substring.
start
Required option. The starting position of the required substring. The index of the first character in the string is 0.
length
Optional. The number of characters that should be included in the returned substring.
If length is 0 or negative, an empty string will be returned. If this parameter is not specified, the substring continues to the end of stringvar.
Here are some related parameters:
str.toLowerCase() converts to lowercase
str.toUpperCase() All strings are converted to uppercase
URL is: Uniform Resource Locator (URL)
The complete URL consists of these parts:
scheme://host:port/path?query#fragment
scheme: Communication protocol
Commonly used http, ftp, maito, etc.
host:host
Server (Computer) Domain Name System (DNS) Host Name or IP Address.
port:port number
Integer, optional, when omitted, the default port of the scheme is used, such as http's default port is 80.
path:path
A string separated by zero or multiple "/" symbols is generally used to represent a directory or file address on the host.
query: query
Optionally, it is used to pass parameters to dynamic web pages (such as web pages made using CGI, ISAPI, PHP/JSP/ASP/ASP.NET and other technologies). There can be multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.
fragment: information fragment
A string that specifies fragments in a network resource. For example, there are multiple noun explanations in a web page, and you can use fragment to directly locate a certain term explanation. (also known as anchor points.)
For such a URL
http://www.maidq.com/index.html?ver=1.0&id=6#imhere
We can get the various parts of it in javascript
1, window.location.href
The entire URl string (the complete address bar in the browser)
Return value in this example: 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
The host part of the URL
Return value in this example: www.maidq.com
4,window.location.port
The port part of the URL
If the default 80 port is used (update: even if:80 is added), the return value is not the default 80 but the empty character
This example returns the value: ""
5, window.location.pathname
The path part 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) part
In addition to assigning values to dynamic languages, we can also give static pages and use javascript to obtain the value of the parameter that is believed to be.
Return value in this example:?ver=1.0&id=6
7,window.location.hash
Anchor point
Return value in this example: #imhere
The above is the full description of the two methods (simple and practical) of JS to obtain address bar parameters introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!