Js url parameter acquisition, setting, modification, and parsing code

Source: Internet
Author: User

In the past, we briefly talked about how to obtain url parameters in js. This article describes how to obtain, set, modify, and parse js url parameters.

Obtain url parameters

The Code is as follows: Copy code

<Script language = javascript>
Function request (paras ){
Var url = location. href;
Var paraString = url. substring (url. indexOf ("? ") + 1, url. length). split ("&");
Var paraObj = {}
For (I = 0; j = paraString [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var returnValue = paraObj [paras. toLowerCase ()];
If (typeof (returnValue) = "undefined "){
Return "";
} Else {
Return returnValue;
}
}
Var theurl = request ('url ');
Var theimg = request ('img ');
Document. writeln ("<a href = '" + theurl + "'target = _ blank> </ a> ");
</Script>

The core code is

 

The Code is as follows: Copy code

Function request (paras ){
Var url = location. href;
Var paraString = url. substring (url. indexOf ("? ") + 1, url. length). split ("&");
Var paraObj = {}
For (I = 0; j = paraString [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var returnValue = paraObj [paras. toLowerCase ()];
If (typeof (returnValue) = "undefined "){
Return "";
} Else {
Return returnValue;
}
}

Set url parameters in js

 

The Code is as follows: Copy code
// Obtain the parameter array in the address bar
Function getUrlParams (){
Var search = window. location. search;
// Write data dictionary
Var tmparray = search. substr (1, search. length). split ("&");
Var paramsArray = new Array;
If (tmparray! = Null ){
For (var I = 0; I <tmparray. length; I ++ ){
Var reg =/[= | ^ =]/; // use = to split, but not =
Var set1 = tmparray [I]. replace (reg ,'&');
Var tmpStr2 = set1.split ('&');
Var array = new Array;
Array [tmpStr2 [0] = tmpStr2 [1];
ParamsArray. push (array );
}
}
// Return the parameter Array
Return paramsArray;
}
// Obtain the parameter value based on the parameter name
Function getParamValue (name ){
Var paramsArray = getUrlParams ();
If (paramsArray! = Null ){
For (var I = 0; I <paramsArray. length; I ++ ){
For (var j in paramsArray [I]) {
If (j = name ){
Return paramsArray [I] [j];
}
}
}
}
Return null;
}

Modify url parameters in js

The Code is as follows: Copy code


Function changeURLPar (url, ref, value ){
Var str = "";
If (url. indexOf ('? ')! =-1)
Str = url. substr (url. indexOf ('? ') + 1 );
Else
Return url + "? "+ Ref +" = "+ value;
Var returnurl = "";
Var setparam = "";
Var arr;
Var modify = "0 ";
If (str. indexOf ('&')! =-1 ){
Arr = str. split ('&');
For (I in arr ){
If (arr [I]. split ('=') [0] = ref ){
Setparam = value;
Modify = "1 ";
}
Else {
Setparam = arr [I]. split ('=') [1];
}
Returnurl = returnurl + arr [I]. split ('=') [0] + "=" + setparam + "&";
}
Returnurl = returnurl. substr (0, returnurl. length-1 );
If (modify = "0 ")
If (returnurl = str)
Returnurl = returnurl + "&" + ref + "=" + value;
}
Else {
If (str. indexOf ('= ')! =-1 ){
Arr = str. split ('= ');
If (arr [0] = ref ){
Setparam = value;
Modify = "1 ";
}
Else {
Setparam = arr [1];
}
Returnurl = arr [0] + "=" + setparam;
If (modify = "0 ")
If (returnurl = str)
Returnurl = returnurl + "&" + ref + "=" + value;
}
Else
Returnurl = ref + "=" + value;
}
Return url. substr (0, url. indexOf ('? ') + "? "+ Returnurl;
}

Parse url parameters in js

The Code is as follows: Copy code


<Script type = "text/javascript">
Function getQueryString (url ){
If (url ){
Url = url. substr (url. indexOf ("? ") + 1); // string truncation, which is more efficient than my previous split () method
}
Var result = {}, // create an object for storing name, and value
QueryString = url | location. search. substring (1), // location. search sets or returns the question mark (?) The start url (query part ).
Re =/([^ & =] +) = ([^ &] *)/g, // regular, not used
M;
While (m = re.exe c (queryString) {// exec () Regular Expression matching, not used
Result [decodeURIComponent (m [1])] = decodeURIComponent (m [2]); // use decodeURIComponent () to decode the encoded URI.
}
Return result;
}
// Demo
Var myParam = getQueryString ("www.taobao.com? Key0 = a & key1 = B & key2 = c ");
Alert (myParam. key1 );
</Script>

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.