Last active
December 22, 2015 21:29
-
-
Save mojaray2k/6533431 to your computer and use it in GitHub Desktop.
This is a good example of how to loop through a group of links and place an icon on external links using jQuery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>External Links with an icon</title> | |
</head> | |
<body> | |
<ul id="links"> | |
<li> | |
<a href="007.html">The previous tip</a> | |
</li> | |
<li> | |
<a href="./009.html">The next tip</a> | |
</li> | |
<!-- External Link: --> | |
<li> | |
<a href="http://www.google.com/">Google</a> | |
</li> | |
</ul> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
<script type="text/javascript" src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loop through all the links | |
$('#links a').each(function() { | |
if (this.hostname != location.hostname) { | |
// The link is external | |
$(this).append('<img src="external.png" />') | |
.attr('target', '_blank'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment