In WEB development, JavaScript is often used to obtain the URL URL information of the current page. Here is some of my brief summary of obtaining URL information.
Let's give an example of a URL and then obtain its components: http://i.cnblogs.com/EditPosts.aspx?opt=1
1. window.location.href (set or get the entire URL as a string)
var test = window.location.href;
alert(test);
Return: http://i.cnblogs.com/EditPosts.aspx?opt=1
2. window.location.protocol (the protocol part of setting or getting URL)
var test = window.location.protocol;
alert(test);
Return: http:
3. window.location.host (set or get the host part of the URL)
var test = window.location.host;
alert(test);
Return to: i.cnblogs.com
4. window.location.port (set or get the port number associated with the URL)
var test = window.location.port;
alert(test);
Return: empty character (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)
5. window.location.pathname (set or get the path part with the URL (that is, the file address))
var test = window.location.pathname;
alert(test);
Return: /EditPosts.aspx
6. window.location.search (set or get the part of the href attribute that follows the question mark)
var test = window.location.search;
alert(test);
Return:?opt=1
PS: Obtain the query (parameter) 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 we believe should.
7. window.location.hash (set or get the segment after the pound sign "#" in the href attribute)
var test = window.location.hash;
alert(test);
Return: empty character (because there is no in url)
8. js get parameter values in url
1. Regular law
function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null;}// Call this way: alert(GetQueryString("parameter name 1")); alert(GetQueryString("parameter name 2")); alert(GetQueryString("parameter name 3"));2. Split splitting method
function GetRequest() { var url = location.search; //Get the string after the "?" character in url var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest;}var Request = new Object();Request = GetRequest();<br>// var id=Request["id"]; // var parameter 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'];3. Designated to take
For example, a url: http://i.cnblogs.com/?j=js. If we want to get the value of parameter j, we can call it through the following function.
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); //Get the string after the "?" character in the url and match it regularly var context = ""; if (r != null) context = r[2]; reg = null; r = null; return context == null || context == "" || context == "undefined" ? "" : context; }alert(GetQueryString("j"));4. Method for obtaining single parameters
function GetRequest() { var url = location.search; //Get the string after the "?" character in url if (url.indexOf("?") != -1) { //Judge whether there are parameters var str = url.substr(1); //From the first character because the 0th is the ? sign to get all strings except the question mark strs = str.split("="); //Separate with equal sign (because I know there is only one parameter, I directly use equal sign to separate if there are multiple parameters to separate with & sign and then use equal sign) alert(strs[1]); //Sign up the first parameter (if there are multiple parameters, it also needs to be looped) }}The above is all about this article. I hope it will be helpful for everyone to understand how to obtain the URL URL information on the current page.