JavaScript gets the method of parameter QueryString in URL _javascript tips

Source: Internet
Author: User

I. Get the querystring parameter of the URL

Two ways to get the querystring parameter of a URL are as follows:

1.1 Method One: regular match

Gets the parameter
function getquerystring (name) {
  var reg = new RegExp (^|&) "+ name +" = ([^&]*) (&|$) in the URL, i) ; Match target parameter
  var result = Window.location.search.substr (1). Match (reg);//querystring matching target parameter
  if (result!= null) C8/>return decodeuricomponent (result[2]);
  else {return
    null;
  }
}

For Http://localhost/index.html?q1=abc&q2=efg&q3=h URLs, the method to get Q1 parameter values is as follows:

var q1 = getquerystring (' Q1 '); Abc

1.2 Method Two: Split

function getquerystring () {  
  var qs = location.search.substr (1),//Get URL "?" Character string  
    args = {},//Save parameter Data Objects
    items = qs.length? Qs.split ("&"): [],//Get each argument item
    = NULL,
    len = Items.length;

  for (var i = 0; i < len; i++) {
    item = items[i].split ("=");
    var name = decodeURIComponent (Item[0]),
      value = decodeURIComponent (item[1));
    if (name) {
      Args[name] = value;
    }
  }
  return args;
}

For Http://localhost/index.html?q1=abc&q2=efg&q3=h URLs, the method to get Q1 parameter values is as follows:

var qs = getquerystring (); 

var q1 = qs["Q1"]; Abc

Both of the above getQueryString() methods can solve the problem of getting the QueryString parameter of the URL well. In this way, arrange a location object to facilitate the future learning reference.

Two. Properties of the Location object

Note: Take Http://localhost:80/dir/index.html?q1=abc&q2=efg&q3=h#anchor as an example:

These 8 attributes of location are both writable and writable.

In which, the change location.href jumps to the new URL page, and the modification location.hash jumps to the anchor position in the current page.

Each modified window.location property (except hash), the page is reloaded with a new URL and a record is generated in the browser's history.

Three. Methods of Location objects

location.assign(url) The effect is exactly the same as the following two lines of code:

window.location = URL;
location.href = URL;

Code that is located location.reload() after the call may or may not be executed, depending on factors such as network latency or system resources. Therefore, it is best to location.reload() put it on the last line of code.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

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.