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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2015 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/2016 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/2016 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/2016 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/2016 03:20
Back to top