js獲取get參數

譬如url爲javascript

http://ttweb.xxxx.com/finance/index.php?r=index/index#report/channelMonthly&cid=17&startT=1509494400&osType=1php

 須要獲取?後面的全部參數,代碼以下:java

 

  function getQueryVariable()
    {
        var query = window.document.location.href.toString();  //http://ttweb.xxxx.com/finance/index.php?r=index/index#report/channelMonthly&cid=17&startT=1509494400&osType=1


        var query = query.split("?");  //問號分割網址 ['http://ttweb.xxxx.com/finance/index.php',' r=index/index#report/channelMonthly&cid=17&startT=1509494400&osType=1']
        var vars = query[1].split("&"); //數組索引爲1的值(r=index/index#report/channelMonthly&cid=17&startT=1509494400&osType=1)以 & 分割,分割結果爲 ( ["r=index/index#report/channelMonthly", "cid=17", "startT=1509494400", "osType=1"])

        var res = [];
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            res[pair[0]] = pair[1];
        }
        return res;
    }

    console.log(getQueryVariable())
相關文章
相關標籤/搜索