|
1 | | -(function () { |
2 | | - const set_language_url = document.currentScript ? |
3 | | - document.currentScript.getAttribute('data-set-lang') : |
4 | | - document.querySelectorAll('script')[document.querySelectorAll('script').length - 1].getAttribute('data-set-lang'); |
| 1 | +(function ($) { |
| 2 | + const set_language_url = (document.currentScript ? |
| 3 | + $(document.currentScript) : |
| 4 | + $('script').last()) // Ugly solution for IE11 |
| 5 | + .attr('data-set-lang'); |
5 | 6 |
|
6 | | - document.addEventListener('DOMContentLoaded', function () { |
7 | | - document.querySelectorAll('a.change-language').forEach(function (elem) { |
8 | | - const target_lang = elem.getAttribute('data-target-lang'); |
9 | | - elem.addEventListener('click', function (event) { |
10 | | - event.preventDefault(); |
11 | | - fetch(set_language_url, { |
12 | | - method: 'POST', |
13 | | - headers: { |
14 | | - 'Content-Type': 'application/json' |
15 | | - }, |
16 | | - body: JSON.stringify({ language: target_lang }) |
17 | | - }) |
18 | | - .then(function (response) { |
19 | | - if (response.ok) { |
20 | | - const href = elem.getAttribute('href'); |
| 7 | + $(function () { |
| 8 | + $('a.change-language').each(function (i, elem) { |
| 9 | + const $elem = $(elem); |
| 10 | + const target_lang = $elem.attr('data-target-lang'); |
| 11 | + $elem.on('click', function () { |
| 12 | + $.post(set_language_url, { language: target_lang }) |
| 13 | + .done(function () { |
| 14 | + const href = $elem.attr('href'); |
| 15 | + // href="#" means "this page, but in another language" |
21 | 16 | if (href === '#') { |
22 | 17 | window.location.reload(); |
23 | 18 | } else { |
24 | 19 | window.location.href = href; |
25 | 20 | } |
26 | | - } |
27 | | - }); |
| 21 | + }); |
| 22 | + return false; |
28 | 23 | }); |
29 | 24 | }); |
30 | 25 | }); |
31 | | -})(); |
| 26 | +})(jQuery); |
0 commit comments