megas
megas

Reputation: 21791

Conflict of two javascript files

I’m playing with html5 LeanBack Player and it works well when on the page only its javascritpts. But if I add these javascript files to rails 3 project then I get error in LeanBack Player's javascript file:

Uncaught TypeError: Object function each(iterator, context) {
  var index = 0;
  try {
    this._each(function(value) {
      iterator.call(context, value, index++);
    });
  } catch (e) {
    if (e != $break) throw e;
  }
  return this;
} has no method ‘split’

in function LBPlayer.prototype.resolveTextPlainSubs.

I guessed that’s because of conflict with native Prototype in rails but I don’t know how to resolve it. Thanks

Upvotes: 1

Views: 413

Answers (1)

Ivo Wetzel
Ivo Wetzel

Reputation: 46745

Indeed it's a conflict with Prototypes extending of Array.prototype:

// leanbackPlayer.js @941
srt = srt.split('\n\n');
var i = 0; var isSub = false;
this.vars.subs[lang] = {};
this.vars.subs[lang].label = {};
this.vars.subs[lang].label = label;
this.vars.subs[lang].track = {};

// Error: using for in over an array is just stupid
//        not even using hasOwnProperty is outright pitiful
for(var s in srt) { 

    // this will also yield `each` but that's a function which has no .split() method
    var st = srt[s].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";

In order to fix it you should replace the for(var s in srt) with a simple for loop:

for(var e = 0, el = srt.length; e < el; e++) {
    var st = srt[e].split('\n');
    var time; var j;
    if(st.length >= 2) {
        var t = "";
        ...
}

Upvotes: 1

Related Questions