2012-07-24 4 views
0

그래서 URL 매개 변수를 캐스팅하는 별도의 테스트를 성공적으로 수행 할 수있었습니다. 하지만 모든 것을 연결하는 데 어려움을 겪고 있습니다.json에서 (클라이언트 서버에있는 동안) 스크립트의 url 매개 변수를 catch하는 방법

여기서 매개 변수는 두 번째 계층에 있습니다. pubID를 var에 넣으면 멋질 것입니다.

누군가가 올바른 방향으로 나를 가리키면 좋을 것입니다.

감사

그래서 내가 그런 로더 스크립트는 것 난 다음 스크립트

<script src="http://api.yoursite.com/js/loader.js?pubID=test123" type="text/javascript"></script> 
<div id="myDiv"></div> 

있을 것 API를 위젯을 구축 할 계획하지만, 클라이언트 서버에서 스크립트에서

을 매개 변수를 얻기에 문제가있어 다음 코드를 작성하십시오.

다음 코드는 loader.js 파일입니다. 이 코드는 위젯 위대한 작품을 다른 사람에 의해 작성되었습니다 만, 문제가 갖는 기본은

(function() { 

// Localize jQuery variable 
var jQuery; 

/******** Load jQuery if not present *********/ 
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.2') { 
var script_tag = document.createElement('script'); 
script_tag.setAttribute("type","text/javascript"); 
script_tag.setAttribute("src", 
    "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"); 
if (script_tag.readyState) { 
    script_tag.onreadystatechange = function() { // For old versions of IE 
     if (this.readyState == 'complete' || this.readyState == 'loaded') { 
      scriptLoadHandler(); 
     } 
    }; 
} else { 
    script_tag.onload = scriptLoadHandler; 
} 
// Try to find the head, otherwise default to the documentElement 
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); 
} else { 
// The jQuery version on the window is the one we want to use 
jQuery = window.jQuery; 
main(); 
} 

/******** Called once jQuery has loaded ******/ 
function scriptLoadHandler() { 
// Restore $ and window.jQuery to their previous values and store the 
// new jQuery in our local jQuery variable 
jQuery = window.jQuery.noConflict(true); 
// Call our main function 
main(); 
} 


/******** Our main function ********/ 
function main() { 
jQuery(document).ready(function($) { 
    /******* Load CSS *******/ 
    var css_link = $("<link>", { 
     rel: "stylesheet", 
     type: "text/css", 
     href: "style.css" 
    }); 
    css_link.appendTo('head'); 



    $.getJSON("http://api.yoursite.com/js/json.php?jsoncallback=?", 
function(data){ 
    $('#myDiv').html(data.name); 
}); 

}); 
} 

})(); // We call our anonymous function immediately 

난 다음 작업 스크립트를 사용하는 매개 변수를 잡으려고 하하 끝낼 수 있습니다.

// Parse URL Queries 
function url_query(query) { 
query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
var expr = "[\\?&]"+query+"=([^&#]*)"; 
var regex = new RegExp(expr); 
var results = regex.exec(window.location.href); 
if(results !== null) { 
    return results[1]; 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} else { 
    return false; 
} 
} 

// Example usage - http://www.kevinleary.net/?load=yes 
var url_param = url_query('pubID'); 
if(url_param) { 
alert(url_param); // "yes" 
} 

답변

관련 문제