2009-01-13 46 views

回答

162

對於GET參數,你可以從document.location.search抓住他們:

var $_GET = {}; 

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function() { 
    function decode(s) { 
     return decodeURIComponent(s.split("+").join(" ")); 
    } 

    $_GET[decode(arguments[1])] = decode(arguments[2]); 
}); 

document.write($_GET["test"]); 

對於POST參數,你可以序列化$_POST對象JSON格式轉換爲<script>標籤:

<script type="text/javascript"> 
var $_POST = <?php echo json_encode($_POST); ?>; 

document.write($_POST["test"]); 
</script> 

當你在它(做服務器端的東西),你可能會聚集到PHP的GET參數,以及:

var $_GET = <?php echo json_encode($_GET); ?>; 

注:你需要PHP 5或更高版本使用內置在json_encode功能。


更新:這裏有一個更通用的實現:

+3

您的提取方法不考慮沒有值的參數(「?foo&bar = baz」=> {foo:「」,bar:「baz」})。 – Gumbo 2009-02-05 15:01:15

+1

漂亮的Gumbo!正則表達式中的=應該是可選的:... snip ...(?:=([^&] *))?&?)/ g – 2009-02-05 16:18:50

+0

1.您的第一個GET示例似乎不適用於我。 2.在您的更新中,您的函數名稱中有拼寫錯誤 – 2009-10-04 02:36:56

15

有一個爲jQuery來獲取Get PARAMS稱爲.getUrlParams

對於POST唯一的解決辦法是呼應POST到一個插件使用PHP的JavaScript變量,如Moran所建議的。

3

使用任何服務器端語言,您必須將POST變量發送到JavaScript。

.NET

var my_post_variable = '<%= Request("post_variable") %>'; 

只是要小心空值。如果你試圖發出的變量實際上是空的,你將得到一個javascript語法錯誤。如果你知道這是一個字符串,你應該用引號包裝它。如果它是一個整數,你可能需要測試一下,看看它是否真的存在,然後再把行寫入到javascript中。

2

這裏有一些東西可以收集全局對象中的所有變量,這是一個多年優化的例程。自從jQuery的興起以來,現在將它們存儲在jQuery本身中似乎是適當的,我們正在與John就一個潛在的核心實現進行檢查。

jQuery.extend({ 
    'Q' : window.location.search.length <= 1 ? {} 
     : function(a){ 
      var i = a.length, 
       r = /%25/g, // Ensure '%' is properly represented 
       h = {};  // (Safari auto-encodes '%', Firefox 1.5 does not) 
      while(i--) { 
       var p = a[i].split('='); 
       h[ p[0] ] = r.test(p[1]) ? decodeURIComponent(p[1]) : p[1]; 
      } 
      return h; 
     }(window.location.search.substr(1).split('&')) 
}); 

用法示例:

switch ($.Q.event) { 
    case 'new' : 
     // http://www.site.com/?event=new 
     $('#NewItemButton').trigger('click'); 
     break; 
    default : 
} 

希望這有助於。 ;)

6

爲什麼不利用好老的PHP?例如,讓我們說我們收到一個GET參數「目標」:

function getTarget() { 
    var targetParam = "<?php echo $_GET['target']; ?>"; 
    //alert(targetParam); 
} 
0

只是爲了記錄在案,我想知道這個問題的答案,所以我使用了PHP方法:

<script> 
var jGets = new Array(); 
<? 
if(isset($_GET)) { 
    foreach($_GET as $key => $val) 
     echo "jGets[\"$key\"]=\"$val\";\n"; 
} 
?> 
</script> 

這樣,我的所有javascript/jquery在此之後運行都可以訪問jGets中的所有內容。它是我感覺很好的優雅解決方案。

1

如果你的$ _GET是多維的,這可能是你在想什麼:

var $_GET = {}; 
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function() { 
    function decode(s) { 
      return decodeURIComponent(s.split("+").join(" ")); 
    } 

    //handling for multidimensional arrays 
    if(decode(arguments[1]).indexOf("[]") > 0){ 
     var newName = decode(arguments[1]).substring(0, decode(arguments[1]).length - 2); 
     if(typeof $_GET[newName] == 'undefined'){ 
      $_GET[newName] = new Array(); 
     } 
     $_GET[newName].push(decode(arguments[2])); 
    }else{ 
     $_GET[decode(arguments[1])] = decode(arguments[2]); 
    } 
}); 
0

我的方法:

var urlParams; 
(window.onpopstate = function() { 
var match, 
     pl  = /\+/g, Regex for replacing addition symbol with a space 
     search = /([^&=]+)=?([^&]*)/g, 
     decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, 
     query = window.location.search.substring(1); 
    urlParams = {}; 
    while (match = search.exec(query)) 
    urlParams[decode(match[1])] = decode(match[2]); 
})(); 
1

簡單,但不失有用從URL獲得瓦爾/值:

function getUrlVars() { 
    var vars = [], hash, hashes = null; 
    if (window.location.href.indexOf("?") && window.location.href.indexOf("&")) { 
     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    } else if (window.location.href.indexOf("?")) { 
     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1); 
    } 
    if (hashes != null) { 
     for (var i = 0; i < hashes.length; i++) { 
      hash = hashes[i].split('='); 
      vars[hash[0]] = hash[1]; 
     } 
    } 
    return vars; 
} 

,我發現它的地方,在互聯網上,只是固定的一些錯誤

1

使用以下功能:

var splitUrl = function() { 
    var vars = [], hash; 
    var url = document.URL.split('?')[0]; 
    var p = document.URL.split('?')[1]; 
    if(p != undefined){ 
     p = p.split('&'); 
     for(var i = 0; i < p.length; i++){ 
      hash = p[i].split('='); 
      vars.push(hash[1]); 
      vars[hash[0]] = hash[1]; 
     } 
    } 
    vars['url'] = url; 
    return vars; 
}; 

和訪問變量vars['index']其中'index'是GET變量的名稱。