Pedro P
Pedro P

Reputation: 373

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.

Upvotes: 0

Views: 4408

Answers (2)

Maxx
Maxx

Reputation: 4072

try

var pathtop = $('#autoplay').find('object').attr('src');
pathtop = pathtop.replace('http://www.youtube.com/v/', '');

Upvotes: 0

Marc B
Marc B

Reputation: 360702

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.

Upvotes: 3

Related Questions