Shaun Mccran

My digital playground

27
J
U
L
2011

Simple JQuery way to detect links pointing to external domains

Ever wanted to detect links on your site that point to other domains? A few times in the past I've had requests to be able to pick out all the hyperlinks from a site that move a user to a different domain.

The code below checks starts with a click event on all anchor links. It then pulls out the hyperlink reference and checks whether it matches the current documents domain. If the destination of the link points to a different domain then you have a block of code that is performed when the user clicks on the link.

view plain print about
1$('a').click(function() {
2
3    var href = $(this).attr('href');
4
5if ((href.match(/^https?\:/i)) && (!href.match(document.domain)))
6 {
7    var extLink = href.replace(/^https?\:\/\//i, '');
8    alert('clicked external link');
9 }
10}

Pretty simple, but it allows you to amend the link with a custom message, or track it in any way you want.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Best Bicycle Lights's Gravatar I simply want to tell you that I am new to weblog and definitely liked this blog site. I have been meaning to write something like this on my website and you have given me an idea.
# Posted By Best Bicycle Lights | 04/10/15 01:58
www.szinf.com's Gravatar I simply want to tell you that I am new to weblog and definitely liked this blog site. I have been meaning to write something like this on my website and you have given me an idea.
# Posted By www.szinf.com | 04/10/15 02:09
http://pou.obtenloya.com/'s Gravatar Ever wanted to detect links on your site that point to other domains? A few times in the past I've had requests to be able to pick out all the hyperlinks from a site that move a user to a different domain.
# Posted By http://pou.obtenloya.com/ | 05/10/15 00:19
Buy USA Facebook Likes's Gravatar I am new to weblog and definitely liked this blog site. I have been meaning to write something like this on my website and you have given me an idea.
# Posted By Buy USA Facebook Likes | 05/10/15 22:46
mains's Gravatar This website sends a lot sample jQuery files to detect links from all domains. These http://www.myeditingandwritingservices.com/ websites are using to get more knowledge and more education to all our blog writers and thank you very much for sharing these essays.
# Posted By mains | 05/10/15 22:56
buy followers's Gravatar I simply want to tell you that I am new to weblog and definitely liked this blog site. I have been meaning to write something like this on my website and you have given me an idea.
more likes on instagram
http://socialgrand.com/buy-instagram-likes/
# Posted By buy followers | 08/10/15 00:03
cardsharing's Gravatar This website sends a lot sample jQuery files to detect links from all domains. These..
# Posted By cardsharing | 08/10/15 00:45
essay writing company's Gravatar This is such an extraordinary asset, to the point that you are giving and you give it away for nothing. I adore seeing blog that comprehend the quality. jQuery is a JavaScript based library that makes utilizing javascript much less demanding for your electronic applications. JQuery is quick, much simple to utilize, little in size and gives part of elements.
# Posted By essay writing company | 13/11/15 02:37
cccam server's Gravatar Rather than writing out long winded table code to display your data in a tabulated fashion why not use the dataTables JQuery plugin to do it for you...
# Posted By cccam server | 15/11/15 03:28
cccam server's Gravatar Thank you for your kind information. Actually I was very much sad that I couldn’t display dynamic data in tables. I have tried many methods and those were all failures. Now I am very happy in getting the perfect way to do this job
# Posted By cccam server | 21/11/15 01:14
amyahlom's Gravatar If we try to determine what appeals to women to males, the final results we get typically point out inside the very same course. You will often recognize that most incredible ladies these days get excited about a relationship which has a cool, that occasionally violations these people verbally and also literally.
http://ethehealthadvisor.com/alpha/
# Posted By amyahlom | 21/11/15 08:31
Plumbing Service's Gravatar If we try to determine what appeals to women to males, the final results we get typically point out inside the very same course.
# Posted By Plumbing Service | 22/11/15 00:29
related site's Gravatar will often recognize that most incredible ladies these days get excited about a relationship which has a cool, that occasionally violatio
# Posted By related site | 24/11/15 02:36
Pechhulp - Blijdatikrij.nl's Gravatar A definitely very good publish by you my close friend. I’ve bookmarked this web page and can occur back again following several days to examine for just about any new posts which you make.
# Posted By Pechhulp - Blijdatikrij.nl | 24/11/15 07:22
Fanzzy's Gravatar It then pulls out the hyperlink reference and checks whether it matches the current documents domain. If the destination of the link points to a different domain then you have a block of code that is performed when the user clicks on the link.
http://www.davieday.ca/mychoice/
# Posted By Fanzzy | 28/11/15 08:04
sleep apnea dot physical's Gravatar whether it matches the current documents domain. If the destination of the link points to a different domain then you have a block of code that is performed when the user clicks on the link.
# Posted By sleep apnea dot physical | 01/12/15 05:34
Essay Services UK's Gravatar This is such an exceptional resource, to the point that you are giving and you give it away in vain. I venerate seeing blog that appreciate the quality. jQuery is a JavaScript based library that makes using javascript substantially less requesting for your electronic applications. JQuery is speedy, much easy to use, little in size and gives some portion of components.
# Posted By Essay Services UK | 21/12/15 22:07
dui attorneys san diego's Gravatar This is an incredible web site and that i cannot suggest you guys enough. packed with helpful resource and nice layout terribly straightforward on the eyes. Please do continue this nice work.
# Posted By dui attorneys san diego | 22/12/15 09:09
Best Essay Writing UK's Gravatar Thank you for showing the secret techniques to detect link on the website. I hope it is going to help in future too.
# Posted By Best Essay Writing UK | 23/12/15 01:21
Law Dissertation Help's Gravatar Detecting links that point to other domains can be a tricky at times but thank you for sharing the code above, hope it will be supportive.
# Posted By Law Dissertation Help | 23/12/15 01:22
Buy Coursework Online UK's Gravatar If the endpoint of the link points to a diverse field then you have a block of code that is completed when the user connects on the link.
# Posted By Buy Coursework Online UK | 23/12/15 01:24
Dissertation Help UK's Gravatar Detecting links on the other websites can be complex but a block of code is required to let the users connect on different links.
# Posted By Dissertation Help UK | 23/12/15 01:25
remedies for tmj pain's Gravatar This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here
# Posted By remedies for tmj pain | 25/12/15 08:22
more help's Gravatar Thank you for showing the secret techniques to detect link on the website. I hope it is going to help in future too.
# Posted By more help | 27/12/15 23:34
Unlock Her Legs Review's Gravatar Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!
# Posted By Unlock Her Legs Review | 04/01/16 01:17
The Bonding Code Review's Gravatar It was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity...
# Posted By The Bonding Code Review | 07/01/16 00:41
Victorino Noval Foundation's Gravatar I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back in the future. All the best...
# Posted By Victorino Noval Foundation | 11/01/16 21:54
Company's Gravatar this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity...
# Posted By Company | 19/01/16 03:20
pure garcinia cambogia's Gravatar keep it up! I’ll go ahead and bookmark your site to come back in the future. All the best...
# Posted By pure garcinia cambogia | 20/01/16 23:36
Mortgage Broker Calgary's Gravatar Oh my goodness! Impressive article dude! Thank you, However I am encountering troubles with your RSS. I don’t know the reason why I am unable to join it. Is there anyone else having similar RSS issues? Anyone who knows the answer will you kindly respond? Thanks!!
http://mortgagebrokercalgarysearch.ca/
# Posted By Mortgage Broker Calgary | 24/01/16 20:22
Cook IVC Filter's Gravatar Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much. and I am looking forward to Hearing from your next..
# Posted By Cook IVC Filter | 25/01/16 02:04
free golf games's Gravatar Your use of formatting when making your points makes your observations very clear and easy to understand. Thank you...
# Posted By free golf games | 03/02/16 02:31
voyance direct's Gravatar The code below checks starts with a click event on all anchor links.
# Posted By voyance direct | 06/02/16 00:33
leads tunnel's Gravatar decade now - and still no one can touch the king in gameplay or subscribers, who himself has been slipping in terms of innovation for some time now
# Posted By leads tunnel | 10/02/16 00:31
places to visit in ajmer's Gravatar king in gameplay or subscribers, who himself has been slipping in terms of innovation for some time now
# Posted By places to visit in ajmer | 14/02/16 02:33
how do you know if you need Zofran heart murmurs l's Gravatar It's hard to find the good from the bad sometimes, but I think you've nailed it! would you mind updating your blog with more information?
best probiotic yogurt's Gravatar There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job...
# Posted By best probiotic yogurt | 19/02/16 23:06
home page's Gravatar still no one can touch the king in gameplay or subscribers, who himself has been slipping in terms of innovation for some time now....
# Posted By home page | 21/02/16 23:48
cccam server's Gravatar A few times in the past I've had requests to be able to pick out all the hyperlinks from a site that move a user to a different domain. http://www.sat-gold.com/
# Posted By cccam server | 22/02/16 02:18
Xarelto hemorrhage lawsuit's Gravatar I have been meaning to write something like this on my website and you have given me an idea.
# Posted By Xarelto hemorrhage lawsuit | 23/02/16 04:29
Back to top