但是它会添加一个历史记录,而我不希望这样。有没有办法重新加载IFRAME而不影响历史记录?
如果使用replace()的话,只有针对您自己域名的iframe才是可选项。 在远程站点上(例如:Twitter按钮)无法正常工作,并且需要一些特定于浏览器的知识来可靠地访问子窗口。
相反,只需删除iframe元素并在同一位置构建一个新元素。 只有在将src属性修改为DOM中的元素之后才会创建历史记录项目,因此请务必在附加之前设置它。
编辑:JDandChips正确指出,可以从DOM中删除,修改和重新附加。 不需要构建全新的元素。
<div ng-repeat="item in ct.items"><iframe src="{{item.url}}"></iframe></div>
。发生的情况是,在angular设置iframe url之前,html尝试使用错误的url加载iframe,如{{item.url}}
,结果是iframe的src被更改,导致浏览器历史记录问题!解决方案是稍后使用jquery创建iframe,在angular渲染所有内容后。 - Fábio Magagnin你可以使用 JavaScript 的 location.replace
方法:
window.location.replace('...html');
用提供的URL替换当前文档。与assign()方法的区别在于,在使用replace()方法后,当前页面不会保存在会话历史记录中,这意味着用户无法使用“后退”按钮导航到它。
window.frames['frame_name'].location
。 - ChrisDiframe.contentWindow.location.replace(href);
。 - Kyle就像 Greg 在上面所说的那样,.replace() 函数可以实现这个功能。我好像无法回复他的答案,但关键是引用 iFrame 的 contentWindow 属性。
var ifr = document.getElementById("iframeId");
ifr.contentWindow.location.replace("newLocation.html");
我刚刚了解到我需要更多的声望才能在回答中进行评论。
另一种重新创建 iframe 的替代方法是从 DOM 中移除 iframe,更改 src 然后重新添加它。
在很多方面,这与 replace()
建议类似,但是当我尝试使用 History.js 并手动管理状态时,遇到了一些问题。
var container = iframe.parent();
iframe.remove();
iframe.attr('src', 'about:blank');
container.append(iframe);
$(modal).one('hidden.bs.modal', function() { $(modal_iframe).attr('src', 'about:blank').addClass('d-none'); var modalParent = $(modal_iframe).parent(); $(modal_iframe).remove(); $(modalParent).append($(modal_iframe)); });
- drew010一个解决方案是使用object
标签而不是iframe
标签。
将其替换为:
<iframe src="http://yourpage"/>
由此:
<object type="text/html" data="http://yourpage"/>
使用此方法可以更新 data
属性而不影响历史记录。如果您使用声明式框架,例如 React.js
,则不应该执行任何命令来更新DOM,这时非常有用。
尝试使用此函数将旧的iframe替换为从旧的iframe复制的新的iframe:
function setIFrameSrc(idFrame, url) {
var originalFrame = document.getElementById(idFrame);
var newFrame = document.createElement("iframe");
newFrame.id = originalFrame.getAttribute("id");
newFrame.width = originalFrame.getAttribute("width");
newFrame.height = originalFrame.getAttribute("height");
newFrame.src = url;
var parent = originalFrame.parentNode;
parent.replaceChild(newFrame, originalFrame);
}
可以像这样使用:
setIFrameSrc("idframe", "test.html");
replace
方法可以绕过将iframe的URL添加到历史记录中的操作:<iframe id="myIframe" width="100%" height="400" src="#"></iframe>
JavaScript:
var ifr = document.getElementById('mIframe')
if (ifr) {
ifr.contentWindow.location.replace('http://www.blabla.com')
}
replace
函数接收的字符串/URL 是什么? - 2540625JDandChips的答案对我在跨域iframe(youtube)上起作用,这是使用vanilla JS(没有JQuery)的代码:
const container = iframe.parentElement;
container.removeChild(iframe);
iframe.setAttribute('src', 'about:blank');
container.appendChild(iframe);
最简单和加载最快的解决方案
使用window.location.replace
在加载页面或框架时不更新历史记录。
对于链接,它看起来像这样:
<a href="#" onclick="YourTarget.location.replace ('http://www.YourPage.com');">
The targeted Link</a>
或者
<a href="javascript:YourTarget.location.replace ('http://www.YourPage.com');">
The targeted Link</a>
但是,如果你希望它不是从链接中触发,而是在加载框架时自动触发,则应该在iframe文件的body部分中放置以下内容:
onload="window.location.replace ('http://www.YourPage.com');"
onload="YourTarget.location.replace ('http://www.YourPage.com');"
注意:对于此脚本,所有的onclick
、onmouseover
、onmouseout
、onload
和href="javascript:"
都可以正常工作。