JS obtains the parameter value in the URL according to the key value and converts the URL parameter to the JSON object _javascript technique

Source: Internet
Author: User

No nonsense, directly affixed to the code, through the example of a description of JS according to the key value of the URL to get the parameter value and the URL of the parameters to convert to JSON object, example two to get URL pass parameters, specific content please see below

Example one:

Converts the parameter portion of a URL into a JSON object

 Parsequerystring:function (URL) {
  var reg_url =/^[^\?] +\? ([\w\w]+) $/,
   Reg_para =/([^&=]+) = ([\w\w]*?) (&|$|#)/g,
   arr_url = reg_url.exec (URL),
   ret = {};
  if (Arr_url && arr_url[1]) {
   var Str_para = arr_url[1], result;
   while (result = Reg_para.exec (Str_para))!= null) {
    ret[result[1]] = result[2];
   }
  return ret;
 }

Gets the value of the parameter in the URL via key

 Getquerystring:function (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;
 }

Example two:

JS obtains the URL pass parameter in two ways:

JS get URL Pass parameter method one:

Here is a JavaScript client solution that gets the URL with questring parameters, equivalent to the ASP's request.querystring,php $_get
Function:

<script language= "javascript" >
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]]= (Strs[i].split ("=") [1]);
  }
 return
 therequest;
}
</Script>

Then we get the corresponding parameter values by calling this function:

<script language= "JavaScript" >
var Request = new Object ();
Request = Getrequest ();
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 '];
</Script>

To get the parameter with the same name that is in the URL string

js get URL Pass parameter method two Regular Analysis Method:

function getquerystring (name) {
  var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I");
  var r = window.location.search.substr (1). Match (reg);
  if (r!=null) return (r[2]); return null;
}
Alert (getquerystring ("parameter name 1"));
Alert (getquerystring ("parameter Name 2"));
Alert (getquerystring ("parameter name 3"));

Other parameters to get an introduction:

Sets or gets the file name or path specified by the object.

alert (window.location.pathname);

Sets or gets the entire URL as a string.

alert (WINDOW.LOCATION.HREF);

Sets or gets the port number associated with the URL.

alert (Window.location.port);

Sets or gets the protocol portion of the URL.

alert (Window.location.protocol);

Sets or gets the segment in the HREF attribute that follows the number "#".

alert (Window.location.hash);

Sets or gets the hostname and port number of the location or URL.

alert (window.location.host);

Sets or gets the portion of the HREF attribute that follows the question mark.

alert (Window.location.search);

The above description of JS based on the key value to get the URL in the parameter value and the URL of the parameters to the JSON object, JS in two ways to get URL pass parameters, code

Very simple, hope to be helpful to everybody.

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.