我正在编写一个将在触摸屏上以信息亭模式运行的网络应用程序。我目前只针对它在 Firefox 3 上运行。我需要访问外部站点的一些用例。我希望使用嵌入式浏览器这样做,我正在使用 <iframe>
解决这个问题.我需要嵌入式主页的后退/前进按钮。
我已经设法访问了 iframe 的历史对象
var w = document.getElementById('embeddedBrowser').contentWindow;
w.history.back();
history
内嵌窗口与父窗口相同。因此对于新加载的 <iframe>
, 这个调用会回到系统的上一个页面。
有什么方法可以避免这种情况或更正确的解决方法吗?
最佳答案
因为每个选项卡中只有一个历史对象共享,所以这似乎是不可能的。正确的解决方法是在回调之前测试 window.history.current
或 window.history.previous
。不幸的是,window.history.current
具有特权,因此对未签名的页面不可用。
这是一个困惑的解决方法的粗略草图:
<iframe src="somepage.html" name="myframe"></iframe>
<p><a href="#" id="backBtn">Back</a></p>
<script type="text/javascript">
document.getElementById('backBtn').onclick = function () {
if (window.frames['myframe'].location.hash !== '#stopper') {
window.history.back();
}
// ... else hide the button?
return false; // pop event bubble
};
window.frames['myframe'].onload = function () {
this.location.hash = 'stopper';
};
</script>
当然,这是假设父窗口中没有(#hash)浏览,等等,但它似乎可以解决限制向后移动的问题。
关于javascript - 带 iframe 的嵌入式浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/244522/