Skip to content

Commit 4c28185

Browse files
committed
Improving Language Selector
Moving the `onchange` function closer to the Element, on the `_includes/languages.html` Places all the methods on a "strict" env inside of a scope When you are in another language that is not English, that language will be selected by default.
1 parent 8c00ee5 commit 4c28185

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

_includes/footer.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@
4747
}
4848
}
4949
};
50-
51-
document.getElementById('languages').onchange = function(ev) {
52-
if (ev.currentTarget && ev.currentTarget.value) {
53-
location.href = ev.currentTarget.value;
54-
}
55-
};
5650
</script>
5751
</body>
5852
</html>

_includes/languages.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,30 @@
44
<option value="{{ lang.path }}">{{ lang.label }}</option>
55
{% endfor %}
66
</select>
7+
8+
<script type="text/javascript">
9+
( function(){
10+
if ( '/' === window.location.pathname ) {
11+
return;
12+
}
13+
14+
var langs = document.getElementById( 'languages' ),
15+
opts = langs.getElementsByTagName( 'option' ),
16+
count = opts.length;
17+
18+
for ( var i = 0; i < count; i++ ){
19+
if ( opts[ i ].value !== window.location.pathname ) {
20+
continue;
21+
}
22+
23+
opts[ i ].setAttribute( 'selected', 'selected' );
24+
}
25+
26+
// Only apply this after we set the Selected option
27+
langs.onchange = function( ev ) {
28+
if ( ev.currentTarget && ev.currentTarget.value ) {
29+
location.href = ev.currentTarget.value;
30+
}
31+
};
32+
} )();
33+
</script>

0 commit comments

Comments
 (0)