JS how to accurately get the current page URL information _javascript tips

Source: Internet
Author: User

In web development, JavaScript is often used to get the URL information for the current page, here is a little summary of some of my get URL information.

Here's an example of a URL, and then get its various 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 (set or get the protocol part of the URL)

var test = Window.location.protocol;
alert (test);
Back: http:

3, Window.location.host (set or get the host part of the URL)

var test = Window.location.host;
alert (test);
return: 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: null character (if the default 80 port is used (update: Even if added: 80), then the return value is not the default 80 but the null character)

5, Window.location.pathname (set or get the URL with the path part (is the file address))
var test = window.location.pathname;
alert (test);
return:/editposts.aspx

6, Window.location.search (set or get the part following the question mark in the href attribute)

var test = Window.location.search;
alert (test);
Back:? opt=1

PS: Get the query (parameters) section, in addition to the dynamic language assignment, we can also give static pages, and use JavaScript to obtain the expected value of the parameter.

7, Window.location.hash (set or get the segment in the href attribute after the # "#")

var test = Window.location.hash;
alert (test);
return: null character (because it is not in the URL)

8, JS get the value of the parameter in the URL

First, 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;
}
This call:
alert (getquerystring ("parameter name 1"));
 
Alert (getquerystring ("parameter Name 2"));
 
Alert (getquerystring ("parameter name 3"));

Split Splitting method

function Getrequest () {
  var url = location.search;//Get URL "?" Character string
  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 '];

Third, the designation takes

For example a url:http://i.cnblogs.com/?j=js, we want the value of parameter J, which can be called by the following function.

function getquerystring (name) { 
  var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); 
  var r = window.location.search.substr (1). Match (REG); Get the "?" in the URL character string and matching the
  var context = ""; 
  if (r!= null) Context 
     = r[2]; 
  reg = null; 
  R = null; 
  return context = NULL | | Context = "" | | context = = "undefined"? "": Context; 
}
Alert (GetQueryString ("J"));

Four, the method of obtaining single parameter

function Getrequest () {
  var url = location.search;//Get URL "?" Character string
  if (Url.indexof ("?")!=-1) {  ///To determine if there is a parameter
   var str = url.substr (1); start with the first character because the No. 0 is the number. Gets all strings except the question mark C18/>strs = Str.split ("=");  Separated by an equal sign (because you know that there is only one argument, so you can separate it with the equals sign if there are multiple parameters separated by a & number and then delimited by an equal sign)
   alert (strs[1]);     POPs the first argument directly (if more than one argument is to be cycled)
  }

The above is the entire content of this article, I hope to understand how to get the current page URL information is helpful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.