【问题标题】:How do I get location for fancybox iframe?如何获取fancybox iframe 的位置?
【发布时间】:2016-04-16 18:05:52
【问题描述】:

我正在为我的弹出窗口使用 Fancybox。每当我创建一个新的 iframe 时,iframe 都会包含一个带有指向其他页面的链接的页面。当用户点击 iframe 中的不同链接并关闭弹出窗口时,我想获取他们访问的最后一个链接。

如何检索上次访问的链接?我只能得到我开始的东西。

$.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 上的示例代码。

【问题讨论】:

    标签: jquery iframe fancybox


    【解决方案1】:

    我提供的示例违反了Same-origin policy

    如果您有一个不违反同源策略的 IFrame,那么您可以执行以下操作来获取用户在 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);
          }
      }
    

    });

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      相关资源
      最近更新 更多