Js get url parameter code instance sharing (JS operation URL) _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to obtain url parameters through js analysis url? In the previous section, # And later sections, you can check the code. The main idea is to parse url parameters into js objects, it is convenient to add, delete, modify, and query operations ~, Take notes here.

The Code is as follows:


Var LG = (function (lg ){
Var objURL = function (url ){
This. ourl = url | window. location. href;
This. href = "";//? Previous section
This. params = {}; // url parameter object
This. jing = ""; // # And later sections
This. init ();
}
// Analyze the url? This. href is saved before, and the parameter is parsed as this. params object.
ObjURL. prototype. init = function (){
Var str = this. ourl;
Var index = str. indexOf ("#");
If (index> 0 ){
This. jing = str. substr (index );
Str = str. substring (0, index );
}
Index = str. indexOf ("? ");
If (index> 0 ){
This. href = str. substring (0, index );
Str = str. substr (index + 1 );
Var parts = str. split ("&");
For (var I = 0; I Var kv = parts [0]. split ("= ");
This. params [kv [0] = kv [1];
}
}
Else {
This. href = this. ourl;
This. params = {};
}
}
// Only modify this. params
ObjURL. prototype. set = function (key, val ){
This. params [key] = val;
}
// Just set this. params
ObjURL. prototype. remove = function (key ){
This. params [key] = undefined;
}
// Url after the three-part Operation
ObjURL. prototype. url = function (){
Var strurl = this. href;
Var objps = []; // The array is used for organizing and then performing the join operation.
For (var k in this. params ){
If (this. params [k]) {
Objps. push (k + "=" + this. params [k]);
}
}
If (objps. length> 0 ){
Strurl + = "? "+ Objps. join ("&");
}
If (this. jing. length> 0 ){
Strurl + = this. jing;
}
Return strurl;
}
// Obtain the parameter value
ObjURL. prototype. get = function (key ){
Return this. params [key];
}
Lg. URL = objURL;
Return lg;
} (LG | {}));

LG is just the namespace of my personal common JS. Call:

The Code is as follows:


Var myurl = new LG. URL ("http://www.baidu.com? A = 1 ");

Myurl. set ("B", "hello"); // added B = hello
Alert (myurl. url ());

Myurl. remove ("B"); // deleted B

Alert (myurl. get ("a"); // obtain the value of parameter a. Here, 1 is obtained.

Myurl. set ("a", 23); // modify the value of a to 23.

Alert (myurl. url ());

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.