Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Friday, September 28, 2012

jQuery code for adding icons to links

Initial use of Blogger ... will see what I think of this tool. First take is there seems to be a lot of junk HTML added to this post, just the kind of thing that compels me to write my own blogging tool. But I'm trying to hold off as (1) it'd be good to be familiar with the mass tools used by others, and (2) the dreaded time factor rarely works in my favor as it is now.

One use I have for this tool is to archive code snippets that I think may help me with future development projects. Noted below is such a piece of code, some jQuery that adds what I'll term "link icons" to a webpage:

$('a').each(function() {
 // external links
 if (this.hostname && this.hostname !== location.hostname)
 {
  $(this).attr({ target:"_blank" });
  $(this).after('');
 }
 // Check for /restricted/ links
 if ((/\/restricted\//i).test($(this).attr('href')) && !(/\/restricted\//i).test(window.location.pathname))
 {
  $(this).after('');
 }
 // Add icons for other document type by extension
 if ((/.pdf$/i).test($(this).attr('href')))
 {
  $(this).attr({ target: "_blank" });
  $(this).after('');
 }
 if ((/.doc[x|m]?$/i).test($(this).attr('href')))
 {
  $(this).after('');
 }
 if ((/.xls[x|m]?$/i).test($(this).attr('href')))
 {
  $(this).after('');
 }
 if ((/.pp[s|t][x|m]?$/i).test($(this).attr('href')))
 {
  $(this).after('');
 }
});

Yahoo.