总是在fancyBox iframe中打开html页面
Always open a html page in fancyBox iframe
我使用Fancybox在Fancybox中显示外部html页面,例如:在page1
中,我设置了一个链接,该链接以Fancybox iframe
模式打开page2
,并且工作良好。
问题是:
是否有可能,如果有人试图进入page2
的URL,使page2
始终从page1
而不是像正常页面一样的浏览器窗口在fancybox中打开?
是的,有可能。
所以第一页(parent.html
),将打开第二页(iframed.html
)在fancybox从一个链接,如
<a id="openIframe" class="fancybox" href="iframed.html">Open page inside fancybox iframe</a>
注意,我设置了一个ID
和一个类链接,将打开框架页面。
然后是每页的脚本:
parent.html
父页应该有两个特定的代码:
- 初始化fancybox的代码
- 一个代码来检测
hash
是否存在于URL中,以便它可以触发fancybox
:
// initialize fancybox
$(".fancybox").fancybox({
// API options
type: "iframe" //<== at least this one
});
if (window.location.hash) {
// detect if URL has hash and trigger fancybox
$(window.location.hash + ".fancybox").trigger("click");
// remove hash after fancybox was opened
// two methods to cover most browsers
if (navigator.userAgent.match(/msie/i)) {
var loc = window.location.href;
index = loc.indexOf('#');
if (index > 0) {
window.location = loc.substring(0, index);
}
} else {
history.pushState("", document.title, window.location.pathname);
};
};
然后第二页(
iframed.html
)应该有一个代码来检测是否本身已经在iframe
中打开了(在我们的情况下是fancybox iframe)。如果没有,它应该重定向到父页面,目标是打开fancybox的链接的ID,所以:
iframed.html
if (self == top) {
window.location.href = "parent.html#openIframe";
};
注意 self == top
将返回true
,如果页面不是在iframe内打开的,所以它重定向到父页面。
请参阅JSFIDDLE(并尝试在新窗口/选项卡中打开链接)
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- 在chrome.tabs.onCreated之后加载HTML页面
- 如何在vs2002中调试html页面
- 使用angular重定向到html页面
- Html页面上的多个Base64图像和平滑加载
- 无法将数据从firebase获取到我的html页面
- 如何在chrome扩展中重定向到html页面
- 如何将值传递到上一个html页面
- AngularJS-需要在index.html页面中访问来自服务的数据
- 如何使用Node/Express显示有关特定错误的自定义html页面
- 如何在 html 页面中调用 jquery
- 加载存储在IndexedDB中的HTML页面
- 同一HTML页面中的两个不同版本的JQuery
- HTML页面如何提取通过表单传递的参数
- 在HTML页面上将URL解析为可读的json格式
- 使用jQuery插入HTML页面的第一个项目缺少结束标记
- 在HTML页面上显示node.js服务器中的数据
- 加载页面时更改html页面的位置
- Ajax调用返回当前html页面,而不是请求的文件
- css是从远程node.js添加的,但不适用于html页面