Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2013, 08:28 PM   PM User | #1
swiltch
New Coder

 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 0 Times in 0 Posts
swiltch is an unknown quantity at this point
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!
swiltch is offline   Reply With Quote
Old 01-31-2013, 08:55 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
thisURL = document.URL;
if(thisURL.indexOf("#") != -1){ // hash tag detected in URL
do stuff
}
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 01-31-2013, 09:12 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:24 AM.


Advertisement
Log in to turn off these ads.