1

i want to replace url with links, the following code work fine but it will harm iframe (e.g. youtube), any advice?thx

$(".css___div p").html($(".css___div p").html().replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a href='$1' target='_blank'>$1</a>"));

update: actually i am doing a bbs, sometime ppl want to post a youtube, so they will make a iframe in the post... for example:

check out my mv!!!
<iframe width="560" height="315" src="http://www.youtube.com/embed/F1C9zIA50Eo" frameborder="0" allowfullscreen></iframe>
http://youtu.be/F1C9zIA50Eo

i want to turn http://youtu.be/F1C9zIA50Eo into a link but not http://www.youtube.com/embed/F1C9zIA50Eo, any hints? thx

update2: finally i did a stupid trick to avoid the code harm iframe:

var var___iframeSRC=$(".css___funk .css___div p iframe").attr("src");
$(".css___div p").html($(".css___div p").html().replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a href='$1' target='_blank'>$1</a>"));
$(".css___funk .css___div p iframe").attr("src",var___iframeSRC);

update3: i put all src to a array... but it still a stupid trick, any advice is welcome

arr___iframeSrc=new Array();
$(".css___funk .css___div p iframe").each(function(){
    arr___iframeSrc.push($(this).attr("src"));
});

$(".css___funk .css___div p").html($(".css___funk .css___div p").html().replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a href='$1' target='_blank'>$1</a>"));

for(var i=0;i<arr___iframeSrc.length;i++){$(".css___funk .css___div p iframe").eq(i).attr("src",arr___iframeSrc[i]);}

1 Answers1

0

you mean you don't want to change the links in frames?

if so, you need to add

if (window.top != window.self) {
    return;
}

to your script

EDIT

ok. from your comment

i want to turn youtu.be/F1C9zIA50Eo into a link but not youtube.com/embed/F1C9zIA50Eo, any hint?

i believe this is what you're looking for:

function replaceURLWithHTMLLinks(text) {
   var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
   return text.replace(exp,"<a href='$1'>$1</a>"); 
}

source: How to replace plain URLs with links?

Community
  • 1
  • 1
RASG
  • 5,988
  • 4
  • 26
  • 47
  • thx but no, actually i am doing a bbs, sometime ppl want to post a youtube, so they will make a iframe in the post... for example: ### check out my mv!!! http://youtu.be/F1C9zIA50Eo ### i want to turn http://youtu.be/F1C9zIA50Eo into a link but not http://www.youtube.com/embed/F1C9zIA50Eo, any hint? thx – facebook-570128180 Apr 24 '12 at 19:48
  • 1
    yes. its very difficult to understand your question. maybe you should start here: [ask] – RASG Apr 24 '12 at 20:48
  • Thank you, first of the first, sorry for my bad English, however, your code (and my code at the top) work fine, except a user embed a youtube video (a iframe). – facebook-570128180 Apr 25 '12 at 17:31