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]);}
2
  • Your update is not a proper solution. What would happen if somebody posts 2 (or more) different iframes?
    – Bergi
    Apr 25, 2012 at 18:38
  • yes... so, any advice... T.T" Apr 26, 2012 at 14:07

1 Answer 1

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?

3
  • 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!!! <iframe width="560" height="315" src="youtube.com/embed/F1C9zIA50Eo" frameborder="0" allowfullscreen></iframe> youtu.be/F1C9zIA50Eo ### i want to turn youtu.be/F1C9zIA50Eo into a link but not youtube.com/embed/F1C9zIA50Eo, any hint? thx Apr 24, 2012 at 19:48
  • 1
    yes. its very difficult to understand your question. maybe you should start here: How to Ask
    – RASG
    Apr 24, 2012 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). Apr 25, 2012 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.