I am using Fancybox for my popups. Whenever I create a new iframe, the iframe contains a page with links to other pages. When the user clicks on different links in the iframe and closes the popup, I would like to get the last link that they have visited.
How can I retrieve the last visited link? I can only get what I started with.
$.fancybox({
type: 'iframe',
href: 'https://slashdot.org/',
beforeClose: function() {
alert('How do I get last link in the frame?');
var name = $(this).prop("content")[0].name;
var iframename = "Frame Name: " + name;
var src = "Frame source: " + $('#' + name).attr('src');
$('#output').html(iframename + "<br/>" + src);
}
});
Sample code on JSFiddle.
The example that I provided was violating Same-origin policy.
If you have an IFrame that does not violate the Same-origin policy, then you can do the following to get the last link the user click on in the IFrame.
$(".iframe_jump").fancybox({
type: 'iframe',
href: 'localFileWithLocalLinks.html',
beforeClose: function() {
// prop("content")[0] will retrieve a HTMLIFrameElement obj
var iframe = $(this).prop("content")[0];
try {
// accessing 'contentWindow' property can trigger an error
var url = iframe.contentWindow.document.location;
var text = "Current window context location: " + url
console.log(text);
} catch(e) {
// log any errors
console.log(e);
}
}
});