2012-12-20 2 views
0

그래서 URL 매개 변수를 가져 와서 내 Javascript 개체의 일부로 사용하려고합니다. 객체 내에서 변수를 추가 할 수 없기 때문에 연결된 연결이없는 것으로 가정합니다. 도움에 미리 감사드립니다.JSON 개체 호출 내 URL 매개 변수 사용

<script> 
    var json = { 
    'c100': { 
    'url': 'https://awesome.com', 
    'heading': 'I am awesome', 
    'category': 'Awesome Junk' 
    }, 
    'c101': { 
    'url': 'https://coolstuff.com', 
    'heading': 'How much cool stuff is there?', 
    'category': 'Cool Junk n Stuff' 
    }, 
    'c102': { 
    'url': 'https://googleorsomething.com', 
    'heading': 'Google is neat.', 
    'category': 'Neat Junk' 
    } 
} 

function GetURLParameter(sParam){ 
    var sPageURL = window.location.search.substring(1); 
    var sURLVariables = sPageURL.split('&'); 
    for (var i = 0; i < sURLVariables.length; i++){ 
    var sParameterName = sURLVariables[i].split('='); 
    if (sParameterName[0] == sParam){ 
     return sParameterName[1]; 
    }; 
    }; 
}; 

// Set Up The Variables for Concatinating the String 
var calcId = GetURLParameter('calc_id'); 
console.log(calcId); 
var jsonObject = 'json.'+calcId+'.url'; 

$(function() { 

    $('#dynamicContent').html(json.calcId.url); 

}); 

답변

1

당신은 []를 사용하여 객체의 속성에 액세스 할 수 있습니다.

var calcId = GetURLParameter('calc_id'); 
var jsonObject = json[calcId]; 

$('#dynamicContent').html(jsonObject.url); 

이 없음 연결이 작업을 위해 필요하지 않습니다.