From ce202874c99071926c082e10b74d2db88e42abb7 Mon Sep 17 00:00:00 2001 From: imaustink Date: Tue, 27 Jun 2017 12:58:24 -0700 Subject: [PATCH] Fixed #352 Clicking links inside search results doesn't work --- static/canjs.js | 7 +++---- static/search.js | 11 +++++++++++ templates/search-results.stache | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/static/canjs.js b/static/canjs.js index 0892a9e6..54769e69 100644 --- a/static/canjs.js +++ b/static/canjs.js @@ -36,13 +36,12 @@ var $articleContainer, // Override link behavior $(document.body).on("click", "a", function(ev) { var noModifierKeys = !ev.altKey && !ev.ctrlKey && !ev.metaKey && !ev.shiftKey, - sameHostname = this.hostname === window.location.hostname, - sameProtocol = this.protocol === window.location.protocol; + sameHostname = (ev.target.hostname || this.hostname) === window.location.hostname, + sameProtocol = (ev.target.protocol || this.protocol) === window.location.protocol; if (noModifierKeys && sameHostname && sameProtocol) { ev.preventDefault(); - var href = this.href; - navigate(href); + navigate(ev.target.href || this.href); } }).on('keyup', 'input[type="checkbox"]', function(e){ var $target = $(e.target); diff --git a/static/search.js b/static/search.js index ea05711a..ba6c5db0 100644 --- a/static/search.js +++ b/static/search.js @@ -516,6 +516,17 @@ var Search = Control.extend({ } return "/" + this.url; + }, + addTargetToExternalURLs: function(html){ + var $html = $('
').html(html); + $html.find('a').each(function(){ + var $a = $(this); + var a = $a[0]; + if(a.hostname !== location.hostname || a.protocol !== location.protocol){ + $a.attr('target', '_blank'); + } + }); + return $html.contents(); } }); diff --git a/templates/search-results.stache b/templates/search-results.stache index d798c18d..14532b05 100644 --- a/templates/search-results.stache +++ b/templates/search-results.stache @@ -30,7 +30,7 @@ {{#if result.description}}
- {{{result.description}}} + {{{addTargetToExternalURLs(result.description)}}}
{{/if}}