CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Detect if URL has anchor tag attached (http://www.codingforums.com/showthread.php?t=286830)

swiltch 01-31-2013 08:28 PM

Detect if URL has anchor tag attached
 
Hey guys,

I'm currently using the following piece of script to add an anchor tag to URL's when they are fed into a page dynamically

Code:

jQuery.fn.mailto = function () {
  return this.each( function() {
    var re = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    $(this).html( $(this).html().replace( re, '<a href="$1" target="_blank">$1</a>' ) );
    var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[\-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=\-]*>))/g;
    $(this).html( $(this).html().replace( re, '<a href="mailto:$1">$1</a>' ) );
    var re = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    $(this).html( $(this).html().replace( re, '$1<a href="http://$2" target="_blank">$2</a>' ) );
});
};

This works fine, BUT the problem I'm having is that some links that are being fed into the page are already active (have the anchor tag already attached to them). This code then adds another anchor tag, which causes problems on the page.

I'm looking for some help in modifying this code to detect that if a URL already has anchor tag then leave it alone, but if it doesn't then add it on so it becomes clickable.

I think this should be quite simple with the use of an if statement but I can't get my head around it.

Any help is really appreciated!

WolfShade 01-31-2013 08:55 PM

thisURL = document.URL;
if(thisURL.indexOf("#") != -1){ // hash tag detected in URL
do stuff
}

felgall 01-31-2013 09:12 PM

The easiest way to test whether that part of the URL already has a value or simply to change it regardless of what value it had before is using location.hash


All times are GMT +1. The time now is 07:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.