Js控制iFrame切换加载网址
<html> <head> <title>Js控制 iFrame 切换加载网址</title> </head> <body> <a href="javascript:jumpto('http://www.163.com')">网易</a> | <a href="javascript:jumpto('http://www.baidu.com)"> 百度搜索</a> | <a href="javascript:jumpto('http://www.skycn.com')">软件下载</a> | <script language="javascript"> <!-- var displaymode = 0 var iframecode = '<iframe id="external" style="width:95%;height:388px" src="http://www.codefans.net/jscss/"></iframe>' if (displaymode == 0) document.write(iframecode) function jumpto(inputurl) { if (document.getElementById && displaymode == 0) document.getElementById("external").src = inputurl else if (document.all && displaymode == 0) document.all.external.src = inputurl else { if (!window.win2 || win2.closed) win2 = window.open(inputurl) else { win2.location = inputurl win2.focus() } } } //--> </script> </body> </html>
gotoWin: function (strUrl) {//在框架中打开页面 var a = document.createElement("a"); a.setAttribute("href", strUrl); a.style.display = "none"; a.setAttribute("target", "iframeOne"); document.body.appendChild(a); if (document.all) { a.click(); } else { var evt = document.createEvent("MouseEvents"); evt.initEvent("click", true, true); a.dispatchEvent(evt); } },
关于IFrame未加载完直接刷新或切换、跳转页面时时会出现Bug问题;
此下方情况无效:location.reload()、location.href = location.href;
需要使用:
frame.src=frame.src
其他情况可以判断iframe是否加载完成 或使用 setTimeout延迟加载