What you can do is to set an onclick
attribute on each link of the iframed page (remote.php) that calls a function in the parent
page and passes their href
as parameter.
You could do this on-the-fly within the fancybox afterShow
callback, so no need to hard-code the attribute in the iframed page.
Then the function in the parent page should close fancybox using the $.fancybox.close()
method and reload the page with the URL passed from the iframed page.
The code that goes in the parent page :
// this function gets the href of the clicked link in the iframed page
// then closes fancybox
// then reloads the current page with the new href
function closeFancybox(href){
jQuery.fancybox.close();
window.location.href = href;
}
jQuery(document).ready(function ($) {
$(".fancybox").fancybox({
afterShow : function () {
// set the onclick attribute on each link of the iframed page
// then passes the corresponding href to the parent page function
$(".fancybox-iframe").contents().find("a").attr("onclick", "parent.closeFancybox(this.href)");
},
// other API options
})
}); // ready
See DEMO at picssel.com and feel free to explore the source code.
Another (cleaner) option, without setting an onclick
attribute, is to bind the click
event directly within the afterShow
callback like :
jQuery(document).ready(function ($) {
$(".fancybox").fancybox({
afterShow : function () {
// bind a click event on each link of the iframed page
// then call the closeFancybox and pass the corresponding href
$(".fancybox-iframe").contents().find("a").on("click", function () {
closeFancybox(this.href);
});
}
})
}); // ready
See alternative DEMO
IMPORTANT: Both solutions are subject to the same-origin policy so it's assumed both pages belong to the same domain.
NOTE : this is for fancybox v2.x