JS obtains the URL parameter value based on the key value and converts the URL parameter to a json object _ javascript

Source: Internet
Author: User
This article focuses on the jsurl parameter value. js obtains the parameter value in the url based on the key value and converts the url parameter to json. if you are interested, let's take a look, this article is not a good place to write. I hope to point out a lot of comments and suggestions, and paste the code directly, example 1 shows how JS obtains the parameter values in the URL based on the key value and converts the URL parameters to json objects. example 2 shows how js obtains url transfer parameters. for details, see the following

Example 1:

// Convert the url parameter part 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; }

// Obtain the parameter values in the url using the 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 2:

Js gets url passing parameters in two ways:

Method 1 of getting url passing parameters in js:

Here is a JAVASCRIPT client solution for obtaining URL parameters with QUESTRING, equivalent to asp's request. querystring, PHP's $ _ GET
Function:

 

Then, we can call this function to obtain the corresponding parameter values:

 

Obtain the parameter with the same name in the url string.

Method 2: 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:

// Set or obtain the specified file name or path of the object.

alert(window.location.pathname);

// Set or retrieve the entire URL as a string.

alert(window.location.href);

// Set or obtain the port number associated with the URL.

alert(window.location.port);

// Set or obtain the URL protocol section.

alert(window.location.protocol);

// Set or obtain the segment following the # In the href attribute.

alert(window.location.hash);

// Set or obtain the hostname and port numbers of the location or URL.

alert(window.location.host);

// Set or obtain the part following the question mark in the href attribute.

alert(window.location.search);

The preceding content introduces how JS obtains the parameter values in the URL based on the key value and converts the URL parameters into json objects. js obtains the url passing parameters in two ways. the code

It is very simple and I hope it will help you.

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.