如何获取 fancybox iframe 的位置?
How do I get location for fancybox iframe?
我正在为我的弹出窗口使用 Fancybox。每当我创建一个新的 iframe 时,iframe 都会包含一个页面,其中 links 到其他页面。当用户单击 iframe 中的不同 link 并关闭弹出窗口时,我想获取他们访问的最后一个 link。
如何检索上次访问的 link?我只能得到我开始的东西。
$.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);
}
});
JSFiddle 上的示例代码。
我提供的例子违反了Same-origin policy。
如果您有一个不违反同源策略的 IFrame,那么您可以执行以下操作以获取用户在 IFrame 中最后一次单击的 link。
$(".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);
}
}
});
我正在为我的弹出窗口使用 Fancybox。每当我创建一个新的 iframe 时,iframe 都会包含一个页面,其中 links 到其他页面。当用户单击 iframe 中的不同 link 并关闭弹出窗口时,我想获取他们访问的最后一个 link。
如何检索上次访问的 link?我只能得到我开始的东西。
$.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);
}
});
JSFiddle 上的示例代码。
我提供的例子违反了Same-origin policy。
如果您有一个不违反同源策略的 IFrame,那么您可以执行以下操作以获取用户在 IFrame 中最后一次单击的 link。
$(".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);
}
}
});