IT story

Javascript를 사용하여 IFrame을 새로 고치는 방법?

hot-time 2020. 8. 7. 07:54
반응형

Javascript를 사용하여 IFrame을 새로 고치는 방법?


IFrame과 Button이있는 웹 페이지가 있습니다. 버튼을 누르면 IFrame을 새로 고쳐야합니다. 가능하다면 어떻게해야합니까? 검색했는데 답을 찾을 수 없었습니다.


var iframe = document.getElementById('youriframe');
iframe.src = iframe.src;

이것은 도움이 될 것입니다 :

document.getElementById('FrameID').contentWindow.location.reload(true);

편집 : @Joro의 설명에 따라 개체 이름을 수정했습니다.


iframe이 동일한 도메인에서로드되는 경우이 작업을 수행 할 수 있습니다.

iframe.contentWindow.location.reload();

IE, Mozzila, Chrome에서 작동

document.getElementById('YOUR IFRAME').contentDocument.location.reload(true);

이 간단한 방법을 사용할 수 있습니다.

function reloadFrame(iFrame) {

    iFrame.parentNode.replaceChild(iFrame.cloneNode(), iFrame);

}

여기 에서 가져 왔습니다

var f = document.getElementById('iframe1');
f.src = f.src;

다음은 HTML 스 니펫입니다.

<td><iframe name="idFrame" id="idFrame" src="chat.txt" width="468" height="300"></iframe></td>

그리고 내 자바 스크립트 코드 :

window.onload = function(){
setInterval(function(){
    parent.frames['idFrame'].location.href = "chat.txt";
},1000);}

src 속성을 직접 재설정 :

iframe.src = iframe.src;

캐시 무효화를위한 타임 스탬프로 src 재설정 :

iframe.src =  iframe.src.split("?")[0] + "?_=" + new Date().getTime();

쿼리 문자열 옵션이 불가능할 때 src 지우기 (데이터 URI) :

var wasSrc = iframe.src

iframe.onload = function() {
    iframe.onload = undefined;
    iframe.src = wasSrc;
}

2017 년 :

동일한 도메인에있는 경우 :

iframe.contentWindow.location.reload();

작동합니다.


해시 전환시 프레임을 새로 고칠 수없는 해시 경로 (예 : mywebsite.com/#/my/url)를 사용하는 경우 :

<script>
    window.onhashchange = function () {
        window.setTimeout(function () {
            let frame = document.getElementById('myFrame');
            if (frame !== null) {frame.replaceWith(frame);}
        }, 1000);
    }
</script>

안타깝게도 시간 제한을 사용하지 않으면 JS는 페이지가 콘텐츠로드를 완료하기 전에 프레임을 교체하려고 시도 할 수 있습니다 (따라서 이전 콘텐츠로드). 아직 해결 방법이 확실하지 않습니다.


페이지 내에 여러 iFrame이있는 경우이 스크립트가 유용 할 수 있습니다. 특정 iFrame을 찾는 데 사용할 수있는 iFrame 소스에 특정 값이 있다고 가정합니다.

var iframes = document.getElementsByTagName('iframe');
var yourIframe = null
for(var i=0; i < iframes.length ;i++){
    var source =  iframes[i].attributes.src.nodeValue;
    if(source.indexOf('/yourSorce') > -1){
        yourIframe = iframes[i];
    }   
}
var iSource = yourIframe.attributes.src.nodeValue;
yourIframe.src = iSource;

"/ yourSource"를 필요한 값으로 바꾸십시오.


iframe의 URL이 변경되지 않으면 다시 만들 수 있습니다.

iframe이 동일한 출처 (프로토콜 체계, 호스트 이름 및 포트)가 아닌 경우 iframe의 현재 URL을 알 수 없으므로 상위 창과 메시지를 교환하려면 iframe 문서에 스크립트가 필요합니다 ( 페이지의 창).

iframe 문서에서 :

window.addEventListener('change', function(e) {
  if (e.data === 'Please reload yourself') {
    var skipCache = true; // true === Shift+F5
    window.location.reload(skipCache);
  }
}

페이지에서 :

var iframe = document.getElementById('my-iframe');
var targetOrigin = iframe.src; // Use '*' if you don't care
iframe.postMessage('Please reload yourself', targetOrigin);

이것을 사용할 수 있습니다 :

Js :

function refreshFrame(){
    $('#myFrame').attr('src', "http://blablab.com?v=");
}

HTML :

`<iframe id="myFrame" src=""></iframe>`

JS Fiddle : https://jsfiddle.net/wpb20vzx/

참고URL : https://stackoverflow.com/questions/2064850/how-to-refresh-an-iframe-using-javascript

반응형