How to get URL value by JS

Source: Internet
Author: User

This article mainly introduced the JS obtains the URL to pass the value the method, the example analyzed the string segmentation and the regular analysis two methods, and supplemented a regular match realization The JS obtains the URL The Get function, needs the friend can refer to the next

JS gets the URL parameter value:

Index.htm? parameter 1 = numeric 1& parameter 2 = numeric 2& parameter 3 = data 3& parameter 4 = numeric 4&

Static HTML file JS read URL parameter control HTML page output based on the parameter value of Get HTML

One, the string segmentation analysis method

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

Function:

1234567891011121314 <Script language="javascript">function GetRequest() {var url = location.search; //获取url中"?"符后的字串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;}</Script>

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

123456789 <script language= "javascript" > var request = new object (); request = Getrequest (); var parameter 1 = request[ ]; parameter 2 = request[ ]; parameter 3 = request[ ]; parameter n = request[ ]; </SCRIPT>

This gets the parameter with the same name in the URL string

Second, the regular analysis method

12345678 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;}alert(GetQueryString("参数名1"));alert(GetQueryString("参数名2"));alert(GetQueryString("参数名3"));

Add: JS Gets the URL of the Get Pass function

123456 function getvl(name) {  var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");  if (reg.test(location.href))  return unescape(RegExp.$2.replace(/\+/g, " "));  return "";};

How to get URL value by JS

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.