2013-11-10 35 views
0

頁面加載我有一個像/accounts/?status=done如何獲得GET數據,當前URL時的jQuery

所以在我想要得到的current urlGET data般的地位文檔加載URL =做

所以下面是我的代碼可以能得到當前的URL,而不是GET數據

<script> 
    $(window).load(function() { 
          //alert("window load occurred!"); 
          var pathname = window.location.pathname; 
        console.log(pathname); 
        $.get(pathname,function(data){ console.log(data) }); 
        }); 
</script> 

那麼如何獲取當前URL和GET樣?狀態數據= jQuery中做了頁面加載?

編輯

於是我根據在這裏提供如下,仍然鏈接已經編輯了無法打印的查詢字符串參數

<script> 
      function getParameterByName(name) { 
       console.log(name+"......................"); 
          name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
          var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
          results = regex.exec(location.search); 
          return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
          } 

    $(window).load(function() { 
          //alert("window load occurred!"); 
          var pathname = window.location.pathname; 
         console.log(pathname+"hurayyyyy"); 
        console.log(pathname); 
        var res = getParameterByName(window.location.pathname) 
       console.log(res+"oooppppppppppppssssssssssss"); 
        }); 


</script> 

結果簡直就像下面

enter image description here

+1

可能dublicate:http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – caramba

+0

我已經加入一個編輯,請移動讓我朝着正確的方向移動。 –

回答

0

在javascript上通過服務器端語言設置值變量,然後在您的JavaScript代碼中使用該變量值。

這裏是PHP服務器端語言代碼: -

var get_status = "<?php echo $_GET['status']; ?>"; 
    if(get_status== 'done'){ 
    //here is your code 
    } 
+0

請在您的答案中正確標記代碼。當你寫答案時,有一個看起來像「{}」的按鈕。選擇一個代碼並按下該按鈕。 –

0

我怎樣才能獲得當前網頁的URL jQuery中

var URL = $(location).attr('href'); 

現在變量URL包含您的瀏覽器的當前地址位置。

0

您也可以嘗試這樣的事:

<script> 
    $(window).load(function() { 
     var URL = $(location).attr('href'); 
     var GET_ARR = URL.split('/'); 
     if($.inArray('?status=done', GET_ARR)){ 
      //do something 
     }      
    }); 
</script>