• js操作url参数


     1 function getQueStr(url, ref) //获取参数值
     2 {
     3     var str = url.substr(url.indexOf('?') + 1);
     4     if (str.indexOf('&') != -1) {
     5         var arr = str.split('&');
     6         for (i in arr) {
     7             if (arr[i].split('=')[0] == ref)
     8                 return arr[i].split('=')[1];
     9         }
    10     }
    11     else {
    12         return url.substr(url.indexOf('=') + 1)
    13     }
    14 }
    15 
    16 function setQueStr(url, ref, value) //设置参数值
    17 {
    18     var str = "";
    19     if (url.indexOf('?') != -1)
    20         str = url.substr(url.indexOf('?') + 1);
    21     else
    22         return url + "?" + ref + "=" + value;
    23     var returnurl = "";
    24     var setparam = "";
    25     var arr;
    26     var modify = "0";
    27 
    28     if (str.indexOf('&') != -1) {
    29         arr = str.split('&');
    30 
    31         for (i in arr) {
    32             if (arr[i].split('=')[0] == ref) {
    33                 setparam = value;
    34                 modify = "1";
    35             }
    36             else {
    37                 setparam = arr[i].split('=')[1];
    38             }
    39             returnurl = returnurl + arr[i].split('=')[0] + "=" + setparam + "&";
    40         }
    41 
    42         returnurl = returnurl.substr(0, returnurl.length - 1);
    43 
    44         if (modify == "0")
    45             if (returnurl == str)
    46                 returnurl = returnurl + "&" + ref + "=" + value;
    47     }
    48     else {
    49         if (str.indexOf('=') != -1) {
    50             arr = str.split('=');
    51 
    52             if (arr[0] == ref) {
    53                 setparam = value;
    54                 modify = "1";
    55             }
    56             else {
    57                 setparam = arr[1];
    58             }
    59             returnurl = arr[0] + "=" + setparam;
    60             if (modify == "0")
    61                 if (returnurl == str)
    62                     returnurl = returnurl + "&" + ref + "=" + value;
    63         }
    64         else
    65             returnurl = ref + "=" + value;
    66     }
    67     return url.substr(0, url.indexOf('?')) + "?" + returnurl;
    68 }
    69 
    70 function delQueStr(url, ref) //删除指定参数值
    71 {
    72     var str = "";
    73 
    74     if (url.indexOf('?') != -1)
    75         str = url.substr(url.indexOf('?') + 1);
    76     else
    77         return url;
    78     var arr = "";
    79     var returnurl = "";
    80     var setparam = "";
    81     if (str.indexOf('&') != -1) {
    82         arr = str.split('&');
    83         for (i in arr) {
    84             if (arr[i].split('=')[0] != ref) {
    85                 returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
    86             }
    87         }
    88         return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
    89     }
    90     else {
    91         arr = str.split('=');
    92         if (arr[0] == ref)
    93             return url.substr(0, url.indexOf('?'));
    94         else
    95             return url;
    96     }
    97 }
    98 var str = window.location.href.replace(/(?|#)[^"]*/g, '');//删除所有参数值
  • 相关阅读:
    学习PyQt5(二):PyQt5布局管理
    学习PyQt5(一):安装PyQt5以及在PyCharm上配置PyQt5
    Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程
    Day036--Python--线程
    Day035--Python--管道, Manager, 进程池, 线程切换
    Day034--Python--锁, 信号量, 事件, 队列, 生产者消费者模型, joinableQueue
    Day033--Python--进程
    Day032--Python--操作系统, process进程
    Day30--Python--struct, socketserver
    Day29--Python--缓冲区, 粘包
  • 原文地址:https://www.cnblogs.com/nicoleyani/p/8757884.html
Copyright © 2020-2023  润新知