JavaScript string replace IE problem
This simple code works fine in FF and Chrome... but not in IE8:
var pathtop = $('#autoplay').find('embed').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');
Gives:
开发者_如何学编程'undefined' is null or not an object error on line 2
I also tried something like this:
pathtop = pathtop.replace('', '');
and the same error!
I am using jQuery in this project.
pathtop on IE is most likely null, because the jquery find/attr chain failed. Split it up into parts and find out which layer ($('#autoplay'), .find()
or .attr()
is returning a null.
Offhand guess - IE's ignoring embed tags in favor of <object>
, so there's no embed in the DOM tree. and you're trying to get the src of a non-existence dom object, making pathtop null, which means there's no replace method available for it.
try
var pathtop = $('#autoplay').find('object').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');
精彩评论