javascript - YouTube Iframe 嵌入导致 IE7 中的重定向

标签 javascript iframe internet-explorer-7 youtube

这段代码适用于除 IE7 之外的所有浏览器。在 IE7 中,用户被重定向到 http://www.youtube.com 。哦,它不只是重定向框架,整个页面都被重定向!有什么想法吗?有想法吗?替代模式?

请帮助我。 IE 正在扼杀我的生存意志。

  $('.youtube .cue a').live('click', function(e) {
    e.preventDefault();
    var $this = $(this);
    var $area = $this.parents('.youtube');
    var $caption = $area.find('.videoDescription');
    var $li = $this.parents('li:first');
    var vid = $.trim($(this).attr('href').replace('#', ''));
    var title = $li.find('.title').text();
    var time = $li.find('.time').text();
    var description = $li.find('.description').text();

    var $frame = $('<iframe></iframe>').attr({
      width:595,
      height:350,
      frameborder: 0,
      src: 'http://www.youtube.com/embed/' + vid
    });

    if (!hasFlash) {
      $area.find('.captioned').html('<a href="http://get.adobe.com/flashplayer/" class="no-flash">Please install Flash</a>.');
    }
    else {
      $area.find('.captioned').html('').append($frame);
    }

    $caption.find('.title').html(title);
    $caption.find('.time').html(time);
    $caption.find('.description').html(description);
  });

最佳答案

在我看来就像这一行:

var vid = $.trim($(this).attr('href').replace('#', ''));

是问题所在。从 <a> 检索 href标签将返回一个完全限定的 URL(包括前面的 http://和域名)。然后,在这一行中,您将其添加到另一个完全限定 URL 的末尾:

src: 'http://www.youtube.com/embed/' + vid

这将产生如下奇怪的结果:

http://www.youtube.com/embed/http://www.domain.com/xxxxxx

对于 iframe src= 属性,这可能不是您想要的。

可能会让您绊倒的是从 <a href="index.html"> 检索 href标签检索完全限定的 URL,即使页面源只有相对 URL。如果您只需要该链接 href 中的路径或文件名,则必须将其解析。

我已经通过重建您的 HTML 和代码来验证,如果您向 iFrame 提供这种错误的 URL,它会重定向整个页面(即使在 Chrome 中)。这是jsfiddle:http://jsfiddle.net/jfriend00/4P3dh/ .

如果您点击“运行”,然后单击“Click Me”链接,由于 iFrame 上的 URL 不正确,它将重定向整个页面。

关于javascript - YouTube Iframe 嵌入导致 IE7 中的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553825/

相关文章:

javascript - html canvas clearRect() 不起作用

html - 隐藏 iframe 上的水平滚动条?

javascript - 如何在 iframe 正文内容 : Javascript 中应用 css

html - 打印包含超过 1 页的表格的 HTML 电子邮件

css - 如何调试IE打印问题

html - 如何针对 ie7 标准定位 css

javascript - Restangular POST 失败 https ://OPTIONS pass

javascript - 进行 CSS 翻转的首选方法是什么?

javascript - Jquery 中的 iframe 未加载页面

javascript - 在函数中插入数组中的 JSON 数据