Using JavaScript to get the value method of the parameters in Request _javascript tips

Source: Internet
Author: User
Tags vars

Suppose you have a URL now, as follows.

Http://www.jb51.net

How to access to the ID and name inside the value of JS, to achieve our analysis of ideas.

First get the URL of the current page, through Window.location.href.

Extract the URL? The following section is through the slice () method.

Divides the fetched request object into an array of strings through the split () method.

Next look at the code.

function Geturlvars () {
  var vars = [],
    hash;
  var hashes = Window.location.href.slice (Window.location.href.indexOf ('? ') + 1). Split (' & ');
  for (var i = 0; i < hashes.length i++) {
    hash = hashes[i].split (' = ');
    Vars.push (Hash[0]);
    Vars[hash[0]] = hash[1];
  }
  return vars;
}

The next step is to get the value of the specified parameter, as shown in the code.

var id = geturlvars () ["id"]; 
var name = Geturlvars () [' Name '];

Finally, let's look at jquery.

$.extend ({
  geturlvars:function () {
    var vars = [],
      hash;
    var hashes = Window.location.href.slice (Window.location.href.indexOf ('? ') + 1). Split (' & ');
    for (var i = 0; i < hashes.length i++) {
      hash = hashes[i].split (' = ');
      Vars.push (Hash[0]);
      Vars[hash[0]] = hash[1];
    }
    return vars;
  },
  geturlvar:function (name) {return
    $.geturlvars () [name];
  }
});
var id = $.geturlvar (' id ');
var name = $.geturlvar (' name ');

The above is a small series for everyone to use JavaScript to get the value of the parameters in request, the full content of the method, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

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.