Skip to main content

jQuery Text to Link Script?



Does anyone know of a script that can select all text references to URLs and automatically replace them with anchor tags pointing to those locations?







For example:



http://www.google.com



would automatically turn into



<a href="http://www.google.com">http://www.google.com</a>







Note: I am wanting this because I don't want to go through all my content and wrap them with anchor tags.



Source: Tips4all

Comments

  1. JQuery isn't going to help you a whole lot here as you're not really concerned with DOM traversal/manipulation (other than creating the anchor tag). If all your URLs were in <p class="url"> tags then perhaps.

    A vanilla JavaScript solution is probably what you want, and as fate would have it, this guy should have you covered.

    ReplyDelete
  2. If you want a solution from another perspective... if you can run the pages through php and HTML Purifier, it can autoformat the output and linkify any urls.

    ReplyDelete
  3. Doing this server-side is not an option sometimes. Think of a client-side Twitter widget (that goes directly to Twitter API using jsonp), and you want to linkify all the URLs in the Tweets dynamically...

    ReplyDelete
  4. You are not precise enough on why you want this but...

    If this is just about lazyness of creating the links, you should really reconsider using JavaScript to dynamically create links on your website. This means that every browser that don't have JavaScript will not be able to use your website.

    There may not be many browsers out there without JavaScript but there are still some important ones like GoogleBot or YahooBot.

    If this is really taking you a long time to create the links, you may consider a server-side alternative... Or maybe a not-so-dynamic solution like a Perl or Ruby script that you could run on your source files before deploying them.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.