Skip to content

Commit 0266fb7

Browse files
authored
Merge pull request #355 from canjs/352-fix-links-in-search-results
Fixed #352 Clicking links inside search results doesn't work
2 parents e9f9d31 + ce20287 commit 0266fb7

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

static/canjs.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ var $articleContainer,
3636
// Override link behavior
3737
$(document.body).on("click", "a", function(ev) {
3838
var noModifierKeys = !ev.altKey && !ev.ctrlKey && !ev.metaKey && !ev.shiftKey,
39-
sameHostname = this.hostname === window.location.hostname,
40-
sameProtocol = this.protocol === window.location.protocol;
39+
sameHostname = (ev.target.hostname || this.hostname) === window.location.hostname,
40+
sameProtocol = (ev.target.protocol || this.protocol) === window.location.protocol;
4141

4242
if (noModifierKeys && sameHostname && sameProtocol) {
4343
ev.preventDefault();
44-
var href = this.href;
45-
navigate(href);
44+
navigate(ev.target.href || this.href);
4645
}
4746
}).on('keyup', 'input[type="checkbox"]', function(e){
4847
var $target = $(e.target);

static/search.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@ var Search = Control.extend({
516516
}
517517

518518
return "/" + this.url;
519+
},
520+
addTargetToExternalURLs: function(html){
521+
var $html = $('<div>').html(html);
522+
$html.find('a').each(function(){
523+
var $a = $(this);
524+
var a = $a[0];
525+
if(a.hostname !== location.hostname || a.protocol !== location.protocol){
526+
$a.attr('target', '_blank');
527+
}
528+
});
529+
return $html.contents();
519530
}
520531
});
521532

templates/search-results.stache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
{{#if result.description}}
3232
<div class="result-description" title="{{result.description}}">
33-
{{{result.description}}}
33+
{{{addTargetToExternalURLs(result.description)}}}
3434
</div>
3535
{{/if}}
3636
</a>

0 commit comments

Comments
 (0)