动态设置iframe src

时间:2011-05-14 09:40:01

标签: javascript jquery

我有一个程序会动态设置iframe src来加载页面。我需要为完全加载的页面挂钩一个事件处理程序。我该怎么做?谢谢!

6 个答案:

答案 0 :(得分:94)

<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/

的示例

答案 1 :(得分:10)

试试这个......

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>

答案 2 :(得分:8)

试试这个:

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

答案 3 :(得分:2)

尝试此代码。然后 'formId' DIV可以设置图像。

$('#formId').append('<iframe style="width: 100%;height: 500px" src="/document_path/name.jpg"' +
 'title="description"> </iframe> ');

答案 4 :(得分:1)

试试这个:

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

答案 5 :(得分:1)

您还应该考虑在某些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

借来的代码