Skip to content Skip to sidebar Skip to footer

Detecting And Changing Mp3 Links JavaScript

I need help changing 's with a href ending in the .mp3 extension from to

Solution 1:

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" ) )

Solution 2:

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/


Post a Comment for "Detecting And Changing Mp3 Links JavaScript"