Javascript + Jq sample code for getting URL parameters in a centralized method _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to obtain URL parameters in a centralized manner using Js + Jq. For more information, see the JQ value method:

Jquery itself does not have a method to get URL parameters, but there is already a plug-in, you can directly get URL and other parameters
Plug-in connection home: https://github.com/allmarkedup/jQuery-URL-Parser
Download link: http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip

Examples of use
Using the current page's url (for these examples https://mysite.com/information/about/index.html? ItemID = 2 & user = dave ):

// Get the protocol
JQuery. url. attr ("protocol") // returns 'http'

// Get the path
JQuery. url. attr ("path") // returns '/information/about/index.html'


// Get the host
JQuery. url. attr ("host") // returns 'mysite. com'

// Get the value for the itemID query parameter
JQuery. url. param ("itemID") // returns 2

// Get the second segment from the url path
JQuery. url. segment (2) // returns 'about'
Using a different url to the current page:

// Set a different URL and return the anchor string
JQuery. url. setUrl ("http://allmarkedup.com/category/javascript/#footer"). attr ("anchor") // returns 'footer'

JS native acquisition:

The most primitive JS method:

The Code is as follows:


Var URLParams = new Array ();
Var aParams = document. location. search. substr (1). split ('&');
For (I = 0; I <aParams. length; I ++ ){
Var aParam = aParams [I]. split ('= ');
URLParams [aParam [0] = aParam [1];
}


Call this method as follows:

Http: // 127.0.0.1/index. php? Name = name1 & cid = 123
// Obtain the passed name parameter
Name = URLParams ["name"];

Document. write (name );
// Obtain the passed cid

Cid = URLParams ["cid"];

Regular Expression Analysis:

Method 1:

The Code is as follows:


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;
}



Call this method as follows:

The Code is as follows:


Alert (GetQueryString ("parameter name 1 "));

Alert (GetQueryString ("parameter name 2 "));

Alert (GetQueryString ("parameter name 3 "));


Method 2:

The Code is as follows:




Call this method as follows:

The Code is as follows:


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.