Skip to content

Commit 4b24e87

Browse files
committed
Adding JS for External Links
1 parent 4f5a4e5 commit 4b24e87

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

_layouts/default.html

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
<!doctype html>
2-
<html>
2+
<html class="no-js">
33
<head>
44
<meta charset="utf-8">
55
<title>{% if page.title %}{{ page.title }} | {% endif %}{{ site.name }}</title>
66
<meta name="description" content="{{ site.description }}">
77
<meta name="viewport" content="width=device-width,initial-scale=1">
8-
8+
99
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
1010
<link rel="stylesheet" href="{{ '/styles/styles.drupal-pattern-lab.css?v=' | append: site.github.build_revision | relative_url }}">
1111
</head>
1212
<body>
13-
13+
1414
<header class="c-global-header">
1515
<div class="o-flag c-global-header__intro">
1616
<div class="o-flag__image">
1717
<a href="/" alt="Home">
1818
<img src="{{ '/assets/images/logo--inverted.svg' | relative_url }}" alt="Pattern Lab logo combined with the Drupal Drop" width="100">
1919
</a>
2020
</div>
21-
21+
2222
<div class="o-flag__body c-global-header__intro-text">
2323
<h1 class="c-project-name">{{ site.name | default: site.github.repository_name }}</h1>
2424
</div>
2525
</div>
26-
26+
2727
<h2 class="c-project-tagline">{{ site.description | default: site.github.project_tagline }}</h2>
28-
29-
28+
29+
3030
{% for nav_item in site.navItems %}
3131
<a href="{{ nav_item.url }}" class="c-btn">{{ nav_item.text }}</a>
3232
{% endfor %}
3333
</header>
34-
34+
3535

3636

3737
<section class="c-page">
@@ -46,6 +46,8 @@ <h2 class="c-project-tagline">{{ site.description | default: site.github.project
4646
</footer>
4747
</section>
4848

49+
<script src="/assets/script.js" defer></script>
50+
4951
{% if site.google_analytics %}
5052
<script type="text/javascript">
5153
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@@ -57,5 +59,5 @@ <h2 class="c-project-tagline">{{ site.description | default: site.github.project
5759
</script>
5860
{% endif %}
5961
</body>
60-
62+
6163
</html>

assets/script.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Query selector using Vanilla JS and `querySelectorAll`, returning Array instead of NodeList..
3+
* @param {String} selector
4+
* @param {HTMLElement} [el=document] - Element to search within.
5+
* @returns {Array}
6+
*/
7+
function query(selector, el) {
8+
el = el || document;
9+
// returning an Array instead of a NodeList
10+
return Array.prototype.slice.call(el.querySelectorAll(selector));
11+
}
12+
13+
function globalSetup() {
14+
document.documentElement.classList.remove('no-js');
15+
document.documentElement.classList.add('js');
16+
17+
// External links open in new tab
18+
query('a[href^=http]').forEach(function (el) {
19+
el.setAttribute('target', '_blank');
20+
});
21+
}
22+
23+
document.addEventListener('DOMContentLoaded', function () {
24+
globalSetup();
25+
});

0 commit comments

Comments
 (0)