Nice programing

iframe src를 동적으로 설정

nicepro 2021. 1. 10. 19:57
반응형

iframe src를 동적으로 설정


페이지를로드하기 위해 iframe src를 동적으로 설정하는 프로그램이 있습니다. 완전히로드 된 페이지에 대한 이벤트 처리기를 연결해야합니다. 내가 어떻게 해? 감사!


<script type="text/javascript">
function iframeDidLoad() {
    alert('Done');
}

function newSite() {
    var sites = ['http://getprismatic.com',
                 'http://gizmodo.com/',
                 'http://lifehacker.com/']

    document.getElementById('myIframe').src = sites[Math.floor(Math.random() * sites.length)];
}    
</script>
<input type="button" value="Change site" onClick="newSite()" />
<iframe id="myIframe" src="http://getprismatic.com/" onLoad="iframeDidLoad();"></iframe>

http://jsfiddle.net/MALuP/의


이 시도...

function urlChange(url) {
    var site = url+'?toolbar=0&amp;navpanes=0&amp;scrollbar=0';
    document.getElementById('iFrameName').src = site;
}

<a href="javascript:void(0);" onClick="urlChange('www.mypdf.com/test.pdf')">TEST </a>

이 시도:

top.document.getElementById('AppFrame').setAttribute("src",fullPath);

이 시도:

document.frames["myiframe"].onload = function(){
   alert("Hello World");
}

또한 일부 Opera 버전에서는 onload가 여러 번 실행되고 일부 후크를 추가해야합니다.

// fixing Opera 9.26, 10.00
if (doc.readyState && doc.readyState != 'complete') {
    // Opera fires load event multiple times
    // Even when the DOM is not ready yet
    // this fix should not affect other browsers
    return;
}

// fixing Opera 9.64
if (doc.body && doc.body.innerHTML == "false") {
    // In Opera 9.64 event was fired second time
    // when body.innerHTML changed from false
    // to server response approx. after 1 sec
    return;
}

Ajax Upload 에서 차용 한 코드

참조 URL : https://stackoverflow.com/questions/6000987/dynamically-set-iframe-src

반응형