2011-05-04 3 views
6

GET으로 전달중인 일부 VAR과 함께 URL을 사용하여 공식적으로 생성 한 iframe이 있습니다. 소스 페이지를 설정하고 변수를 POST 또는 다른 것으로 전달할 수 있습니까? 또는 실제로, 부모 변수에 저장된 변수에 어떻게 든 접근 할 수 있다면 그 점도 좋을 것입니다.GET을 사용하지 않는 iframe에 값 전달

답변

5

사용 window.parent를 사용하여 iframe을에서 컨테이너 페이지에 정의 된 변수에 액세스 할 수 있습니다. iframe을에서

window.passingObj = { "key" : "value" }; 

: 부모 창에서

alert(window.parent.passingObj["key"]); // value 
0

자바 스크립트에서만 액세스가 필요한 경우 hash을 사용할 수 있습니다.

다음 페이지에서 방금 location.hash.substring(1)에서 추출하면됩니다.

var query = (function(input){ 
    input = input.substring(1).split("&"); 
    var data = {}, pair, i = input.length; 
    while (i--) { 
     pair = input[i].split("="); 
     data[pair[0]] = decodeURIComponent(pair[1]); 
    } 
    return data; 
}(location.hash); 

query.arg1; // 1 
query.arg2; // 2 
0

두 페이지가 모두 동일한 도메인에 있으면 iframe이 내부에서

parent.varName 
0
당신은 단순히 부모 물체를 사용하여 iframe 대응의 부모 창에서 변수와 방법을 사용할 수 있습니다

; 상위 창에서이있는 경우, 예를 들면 :

var someObject = {key1:'value1' , key2:'value2'}; 
function test(key) 
{ 
    alert(someObject[key]); 
} 

당신은 자식 iframe에이 작업을 수행 할 수 있습니다

parent.test('key1'); 
alert(parent.someObject.key2); 

가 명심

iframe이 그 부모에 속하는 경우에만 가능합니다 동일한 도메인 (동일 출처 정책).

0

내가 말하는 것은 매개 변수를 전달하기 위해 URL을 사용하고 싶지 않다는 것입니다.

은 자바 스크립트가 실행되는 위치에 따라 다음과 같은 옵션이 있습니다 컨테이너 창에서 실행하면 iframe이 윈도우에서 실행되는 경우이

... 
var el = document.getElementById('targetFrameId'); 

getIframeWindow(el).someFunction('value1', 'value2', ...); 
// or 
getIframeWindow(el).someVariable = {key1:'value1', key2:'value2', ...}; 
... 
  • 같은 것을 할 것

    • 당신 이 같은 것을 할 것입니다.

      ... window.parent.something ... ; 
      

    별관

    function getIframeWindow(iframe_object) { 
        var doc; 
    
        if (iframe_object.contentWindow) { 
        return iframe_object.contentWindow; 
        } 
    
        if (iframe_object.window) { 
        return iframe_object.window; 
        } 
    
        if (!doc && iframe_object.contentDocument) { 
        doc = iframe_object.contentDocument; 
        } 
    
        if (!doc && iframe_object.document) { 
        doc = iframe_object.document; 
        } 
    
        if (doc && doc.defaultView) { 
        return doc.defaultView; 
        } 
    
        if (doc && doc.parentWindow) { 
        return doc.parentWindow; 
        } 
    
        return undefined; 
    }