JoshMWilliams
JoshMWilliams

Reputation: 2384

Detecting and Changing mp3 links javaScript

I need help changing

<a>'s with a href ending in the .mp3 extension from <a href="audioLink"></a> 

to

<embed type="application/x-shockwave-flash" flashvars="audioUrl=audioLink.mp3" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="500" height="27" quality="best"></embed>

using JavaScript

Upvotes: 1

Views: 678

Answers (2)

Minko Gechev
Minko Gechev

Reputation: 25682

Here is sample solution:

var links = $(document.body).find('a'), embeded;
for (var i = 0; i < links.length; i+=1) {
    if ($(links[i]).attr('href').indexOf('.mp3') >= 0) {
        embeded = $('<embed type="application/x-shockwave-flash" flashvars="audioUrl='+ $(links[i]).attr('href') +'" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="500" height="27" quality="best"></embed>');
        embeded.insertBefore($(links[i]));
        $(links[i]).remove();
    }
}

You can see it working here: http://jsfiddle.net/4JqaA/

Upvotes: 0

abuduba
abuduba

Reputation: 5042

By either - jQuery

$("a").each(function(){ 
   var link = $(this).attr("href");
   if(~link.indexOf('.mp3')) {
           $("<embed type='application/x-shockwave-flash' flashvars='audioUrl="+link+"' src='http://www.google.com/reader/ui/3523697345-audio-player.swf' width='500' height='27' quality='best'></embed>").insertAfter(this);
            $(this).remove();
    }

 })

or pure Javascript

(function( l ){ 
    for( var i=l.length; i--;)
        var link = l[i];
        if(~link.href.indexOf('.mp3')) {
           var p = link.parentNode,
               embed = document.createElement("embed"),
               attr = {  type:'application/x-shockwave-flash',
                         flashvars: 'audioUrl='+link.href,
                         src:'http://www.google.com/reader/ui/3523697345-audio-player.swf',
                         width:'500',
                         height:'27',
                         quality:'best'}

           for(var j in attr)
               embed[j]=attr[j]
           p.insertBefore(embed,link);
           p.removeChild(link);

        }


})( document.getElementsByTagName( "a" ) )

Upvotes: 3

Related Questions