extlink.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // $Id: extlink.js,v 1.8 2010/05/26 01:25:56 quicksketch Exp $
  2. (function ($) {
  3. function extlinkAttach(context) {
  4. // Strip the host name down, removing ports, subdomains, or www.
  5. var pattern = /^(([^\/:]+?\.)*)([^\.:]{4,})((\.[a-z]{1,4})*)(:[0-9]{1,5})?$/;
  6. var host = window.location.host.replace(pattern, '$3$4');
  7. var subdomain = window.location.host.replace(pattern, '$1');
  8. // Determine what subdomains are considered internal.
  9. if (Drupal.settings.extlink.extSubdomains) {
  10. var subdomains = "([^/]*\\.)?";
  11. }
  12. else if (subdomain == 'www.' || subdomain == '') {
  13. var subdomains = "(www\\.)?";
  14. }
  15. else {
  16. var subdomains = subdomain.replace(".", "\\.");
  17. }
  18. // Build regular expressions that define an internal link.
  19. var internal_link = new RegExp("^https?://" + subdomains + host, "i");
  20. // Extra internal link matching.
  21. var extInclude = false;
  22. if (Drupal.settings.extlink.extInclude) {
  23. extInclude = new RegExp(Drupal.settings.extlink.extInclude.replace(/\\/, '\\'));
  24. }
  25. // Extra external link matching.
  26. var extExclude = false;
  27. if (Drupal.settings.extlink.extExclude) {
  28. extExclude = new RegExp(Drupal.settings.extlink.extExclude.replace(/\\/, '\\'));
  29. }
  30. // Find all links which are NOT internal and begin with http (as opposed
  31. // to ftp://, javascript:, etc. other kinds of links.
  32. // When operating on the 'this' variable, the host has been appended to
  33. // all links by the browser, even local ones.
  34. // In jQuery 1.1 and higher, we'd use a filter method here, but it is not
  35. // available in jQuery 1.0 (Drupal 5 default).
  36. var external_links = new Array();
  37. var mailto_links = new Array();
  38. $("a:not(." + Drupal.settings.extlink.extClass + ", ." + Drupal.settings.extlink.mailtoClass + ")", context).each(function(el) {
  39. try {
  40. var url = this.href.toLowerCase();
  41. if (url.indexOf('http') == 0 && (!url.match(internal_link) || (extInclude && url.match(extInclude))) && !(extExclude && url.match(extExclude))) {
  42. external_links.push(this);
  43. }
  44. else if (url.indexOf('mailto:') == 0) {
  45. mailto_links.push(this);
  46. }
  47. }
  48. // IE7 throws errors often when dealing with irregular links, such as:
  49. // <a href="node/10"></a> Empty tags.
  50. // <a href="http://user:pass@example.com">example</a> User:pass syntax.
  51. catch(error) {
  52. return false;
  53. }
  54. });
  55. if (Drupal.settings.extlink.extClass) {
  56. // Apply the "ext" class to all links not containing images.
  57. if (parseFloat($().jquery) < 1.2) {
  58. $(external_links).not('[img]').addClass(Drupal.settings.extlink.extClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.extClass + '></span>'); });
  59. }
  60. else {
  61. $(external_links).not($(external_links).find('img').parents('a')).addClass(Drupal.settings.extlink.extClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.extClass + '></span>'); });
  62. }
  63. }
  64. if (Drupal.settings.extlink.mailtoClass) {
  65. // Apply the "mailto" class to all mailto links not containing images.
  66. if (parseFloat($().jquery) < 1.2) {
  67. $(mailto_links).not('[img]').addClass(Drupal.settings.extlink.mailtoClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.mailtoClass + '></span>'); });
  68. }
  69. else {
  70. $(mailto_links).not($(mailto_links).find('img').parents('a')).addClass(Drupal.settings.extlink.mailtoClass).each(function() { if ($(this).css('display') == 'inline') $(this).after('<span class=' + Drupal.settings.extlink.mailtoClass + '></span>'); });
  71. }
  72. }
  73. if (Drupal.settings.extlink.extTarget) {
  74. // Apply the target attribute to all links.
  75. $(external_links).attr('target', Drupal.settings.extlink.extTarget);
  76. }
  77. if (Drupal.settings.extlink.extAlert) {
  78. // Add pop-up click-through dialog.
  79. $(external_links).click(function(e) {
  80. return confirm(Drupal.settings.extlink.extAlertText);
  81. });
  82. }
  83. // Work around for Internet Explorer box model problems.
  84. if (($.support && !($.support.boxModel === undefined) && !$.support.boxModel) || ($.browser.msie && parseInt($.browser.version) <= 7)) {
  85. $('span.ext, span.mailto').css('display', 'inline-block');
  86. }
  87. }
  88. Drupal.behaviors.extlink = {
  89. attach: function(context){
  90. extlinkAttach(context);
  91. }
  92. }
  93. })(jQuery);