2011-11-17 3 views
4
당신에게 표시 매개 변수를 통해 사이클을 어떻게

지금 jQuery를 함께 내가 페이지로 전달 된 GET 변수를 모두 표시하고 싶은 당신이는 jQuery를

http://www.somesite.com/somepage.html?id=1&page=2&field=3 같은 URL을 가지고 가정하자. 이걸 어떻게하면 돼? 그것의 벌금 (아마도 더 나은 경우) 일부 디버그 기능의 일환으로 이루어집니다.

답변

1

window.location.href 변수를 살펴볼 수 있습니다.

http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html는 기본적으로 getUrlVars으로 jQuery를 확장 :

$.extend({ 
    getUrlVars: function(){ 
    var vars = [], hash; 
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
    for(var i = 0; i < hashes.length; i++) 
    { 
     hash = hashes[i].split('='); 
     vars.push(hash[0]); 
     vars[hash[0]] = hash[1]; 
    } 
    return vars; 
    }, 
    getUrlVar: function(name){ 
    return $.getUrlVars()[name]; 
    } 
}); 

이 그 다음을 사용 :

var allVars = $.getUrlVars(); 
var byName = $.getUrlVar('name'); 
1

당신은 window.location.search을 사용할 수 있습니다

여기 당신이 원하는 것을 정확히 수행하는 좋은 가이드입니다. 여기 개체로 매개 변수를 반환하는 빠른 함수의 :

function getParameters() { 
    var parameters = {}; 
    var splitStr = window.location.search.substring(1).split(/[&=]/); 
    var i, key, value; 

    for(i = 0; typeof (key = splitStr[i], value = splitStr[i + 1]) !== 'undefined'); i += 2) { 
     parameters[decodeURIComponent(key)] = decodeURIComponent(value); 
    } 

    return parameters; 
} 
2
(function(){ 

var $_GET = {}; 

jQuery.each(document.location.search.substr(1).split("&"), function(index, value){ 
var split = value.split("="); 
$_GET[decodeURIComponent(split[0])] = decodeURIComponent(split[1]); 
}); 

window.$_GET = $_GET; 

})(); 
+0

우리를 보자는 (http://chat.stackoverflow.com/rooms/5079/discussion-between- [채팅에서이 토론을 계속] minitech-and-esailija) – Ryan