JS URL parameters Get, set, modify, parse code

Source: Internet
Author: User

Get 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> ");
</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;
}
}

JS Set URL parameters

The code is as follows Copy Code
Gets an array of arguments for 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 =/[=|^==]/; Split with =, excluding = =
var Set1 = tmparray[i].replace (Reg, ' & ');
var tmpStr2 = set1.split (' & ');
var array = new Array;
Array[tmpstr2[0]] = tmpstr2[1];
Paramsarray.push (array);
}
}
Returns the array of arguments
return paramsarray;
}
Get parameter values based on parameter names
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;
}

JS Modify URL Parameters

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

JS Parse URL parameter

The code is as follows Copy Code


< Script type= "Text/javascript" >
function getquerystring (URL) {
if (URL) {
Url=url.substr (Url.indexof ("?") +1); String interception, more efficient than my previous split () method
}
var result = {},//Create an object for saving name, and value
QueryString =url | | location.search.su Bstring (1),//location.search Sets or returns the URL (part of the query) starting from the question mark (?).
Re =/([^&=]+) = ([^&]*)/g,//regular, specifically not
m;
while (M = Re.exec (querystring)) {//exec () matching of regular expressions, specifically not
Result[decodeuricomponent (m[1])] = decodeURIComponent (m[2]); Decodes the encoded URI using decodeURIComponent ()
}
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.