Method for js to get url-based value transfer _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to get url-based value passing through javascript. The example analyzes two methods: string segmentation and regular expression analysis, in addition, I added a get value passing function for js to get URLs based on regular expression matching. For more information, see the example in this article. We will share this with you for your reference. The details are as follows:

Js:

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

Static html file js reads url parameters and controls html page output based on the obtained html parameter values

I. string segmentation Analysis

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.

Ii. Regular Expression Analysis

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 ("parameter name 1"); alert (GetQueryString ("parameter name 2 ")); alert (GetQueryString ("parameter name 3 "));

Supplement: js obtains the get value passing function of the url.

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

Simple usage

For example, the url is http: // localhost/index. php? The qz value code in qz = ddddk is as follows:

The Code is as follows:

Getvl (bb); // you can

I hope this article will help you design JavaScript programs.

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.