【问题标题】:TypeError: undefined is not an object - Safari & IE [duplicate]TypeError:未定义不是对象-Safari和IE [重复]
【发布时间】:2016-07-03 16:24:49
【问题描述】:
runAnimation: function(){
  var iframe = document.querySelector('.active iframe');
  window.frames[iframe.id].contentWindow.runAnimation();
},

以上代码在 Safari 中打印如下错误:

TypeError: undefined is not an object (evaluating 'window.frames[iframe.id].contentWindow.runAnimation')

hereherehere的答案我都试过了,基本上所有可能的解决方案。

要指出的另一件有趣的事情是 window 仅在 Safari 上返回 undefined

/**
 * @property {array} Templates.slide - The template part for each slide
 * Html template for app
 */

//Iframe Template
presenter.Templates.slide = [
  '<iframe id="slide-<%= id %>" class="slide" data-disable-back="<%= disableBack %>" data-disable-forward="<%= disableForward %>" data-position="<%= position %>" data-src="<%= url %>">',
].join('\n');




/**
 * Default options for the carousel.
 * @public
 */

//Owl Carousel options
	Owl.Defaults = {
		...
		// Classes and Names
		themeClass: 'owl-theme',
		baseClass: 'owl-carousel',
		itemClass: 'owl-item',
		centerClass: 'center',
		activeClass: 'active'
	};

/**
 * The main view for the presentation
 * This renders a collection presentation
 * Builds owlCarousel and attaches api controls
 */
presenter.PresentationView = Backbone.View.extend({
  ...
  
  /**
   * Runs slide animation. Called on the slide itself once its loaded
   */
  runAnimation: function(){
    var iframe = document.querySelector('.active iframe');
    window.frames[iframe.id].contentWindow.runAnimation();
  },
  ...

/**
 * Adds listners for carousel events. Used to trigger runAnimation along with
 * other function
 */
  carouselEvents: function(){
    var self = this;
    this.slider.on('translated.owl.carousel', function(){
      self.showControls();
      self.runAnimation();
    });
  },
  
/**
 * Standard backbone method. Called when view is instantiated
 * @param {object} options - Options passed when creating the view
 *                           (such as instanceURL)
 */
  initialize : function(options){
    this.options = options;
    this.render();
    this.buildSlider();
    this.loadFour();
    this.carouselControls();
    this.carouselEvents();
    this.keyNavigation();
    this.UIEvent();
    this.reScale();
  },

});

【问题讨论】:

  • 你可能想使用document.querySelector(".active iframe")
  • 我已经试过了:(
  • 刚刚在 IE 中测试过,似乎可以正常工作。你能检查一下我刚刚发布的 plnkr 链接吗
  • 请分享重现问题所需的代码。我们甚至不知道您是否有一个带有活动类或 iframe 的元素......顺便说一句,这与backbone.js 有什么关系?
  • @TJ 对不起,我迷路了。它在 Safari 和 IE 中不起作用。该元素存在是因为它可以在 Chrome+Firefox 中运行,不是吗?

标签: javascript iframe backbone.js safari


【解决方案1】:

我试过这样,它似乎工作

var iframe = document.querySelector('.container iframe');
window.frames[iframe.id].contentWindow.runAnimation();

https://plnkr.co/edit/0pquvlK3beF1pqMcU1lc?p=preview

【讨论】:

  • 是的,它应该也适用于我的情况,但它仅在 Safari 中不起作用。
  • 任何特定版本的 Safari(或操作系统)。对我来说工作得很好
  • Safari 可能存在跨域问题。能否请您从控制台发送所有错误消息
  • 没有其他错误:TypeError: undefined is not an object (evaluating 'window.frames[iframe.id].contentWindow.runAnimation') runAnimationapp.js:17689 (anonymous function)app.js:17750 (anonymous function)
猜你喜欢
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 2012-04-22
  • 2014-07-12
  • 2021-06-11
  • 2018-12-21
  • 2021-12-17
  • 2021-12-01
相关资源
最近更新 更多