Domanda

Il codice che sto usando per ottenere l'ID da un video di Vimeo è questo: Funzione

function vimeo_embed_format(src, width, height) {
  var final_syntax;
  var first_part;
  var second_part;
  var third_part;

  first_part = "<iframe src='http://player.vimeo.com/video/" + src + "' ";
  second_part = "width='" + width + "' height='" + height +"' ";
  third_part = "frameborder='0'></iframe>";
  final_part = first_part + second_part + third_part;

  return final_part;
}

vimeo_id_extract(url) {
  var re;
  var match;
  var final_id;

  re = new RegExp('/[0-9]+', "g");
  match = re.exec(url);

  if(url.toLowerCase().indexOf('vimeo') > 0) {
    if (match == null) {
      return false;
    } else {
      return match[0].substring(1);
    }
  }
}

Ed è così che lo uso:

function vimeo_do(video, width, height) {
  var make_syntax;
  var regexp_url;

  regexp_url = /((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)/;

  if(regexp_url.test(video) == false) {
    make_syntax = vimeo_embed_format(vimeo_id_extract(video), width, height);
  } else {
    make_syntax = vimeo_embed_format(video, width, height);
  }

  document.writeln(make_syntax);
}

Il problema è che mi restituisce undefined. Cosa devo fare per correggere?

È stato utile?

Soluzione

Il problema è questo if(url.toLowerCase().indexOf('vimeo') > 0) Se sei URL è vimeo.com/idnro quindi l'indice di vimeo Nell'URL è 0. Inoltre, probabilmente vorresti modificare il tuo regex per essere re = new RegExp('[0-9]+$') Per tenere conto degli URL come

http://vimeo.com/groups/687/videos/3741327

Altrimenti corrisponderai a 687 e non all'ID video reale. Lo consiglio vivamente anche Firebug Per debug di cose come questa. È una gemma.

AGGIORNARE

Supponendo che entrerà un URL malato vimeo_id_extract Questo è quello che farei. Restituire in primo luogo un numero in entrambe le occasioni dal vimeo_id_extract e verificare che il numero restituito non sia valido. In caso contrario, tutto dovrebbe andare bene, altrimenti non c'era un ID video nell'URL.

Se sai che l'ID video terminerà sempre la stringa, aggiungi un $ Firma fino alla fine del regex per assicurarti che l'ultima sequenza di sette cifre venga estratta.

function vimeo_id_extract(url) {
  var re;
  var match;
  re = new RegExp('[0-9]{7}');
  match = re.exec(url);
  if(match != null) {
    return match[0];
  }
  else {
    return -1;
  }
}

function vimeo_do(video, width, height) {
  var make_syntax;
  var regexp_url;

  regexp_url = /((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)/;

  if(regexp_url.test(video) == false) {
    vimeo_id = vimeo_id_extract(video)
    if vimeo_id != -1 {
      make_syntax = vimeo_embed_format(vimeo_id_extract(video), width, height);
    }
    else {
      // there is no video id in the string passed in
    }
  } else {
    make_syntax = vimeo_embed_format(video, width, height);
  }

  document.writeln(make_syntax);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top