![Check out Enfold](https://kriesi.at/wp-content/themes/kriesi/images/banner-enfold.jpg)
Tagged: javascript
-
AuthorPosts
-
November 3, 2020 at 1:35 pm #1257987
Dear all,
I just updated the Enfold theme to the latest version. Our website is bilingual, where text of each language is stored within a designated span-tag on one and the same page. E.g.:
<span lang=”en”>Hello World!</span><span lang=”de”>Hallo Welt!</span>
A JavaScript in the “Google Analytics Tracking Code” textarea is responsible for displaying the one or the other language (depending on which cookie was set by deciding for a language). Here is the code:
<script> jQuery(document).ready(function(){ if( getCookie("language") == "de" ) { langElements = getAllElementsWithAttributeValue('lang','en'); for (var i in langElements) { langElements[i].style.visibility = 'hidden'; langElements[i].style.display = 'none'; } } else { langElements = getAllElementsWithAttributeValue('lang','de'); for (var i in langElements) { langElements[i].style.visibility = 'hidden'; langElements[i].style.display = 'none'; } } }); function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i <ca.length; i++) { var c = ca[i]; while (c.charAt(0)==' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length,c.length); } } return ""; } function getAllElementsWithAttributeValue(attribute,attributevalue) { var matchingElements = []; var allElements = document.getElementsByTagName('*'); for (var i = 0, n = allElements.length; i < n; i++) { if (allElements[i].getAttribute(attribute) == attributevalue) { // Element exists with attribute. Add to array. matchingElements.push(allElements[i]); } } return matchingElements; } </script>
Since the update this no longer works (I checked out, the code is still in the “Google Analytics Tracking Code” textarea). Why is the script no longer executed? Any suggestions?
Thanks a lot and all the best,
ChristianNovember 8, 2020 at 1:17 am #1259085Hey kristianjohann,
Sorry for the late reply and thanks for the link and code. Looking at your script above I note that it is looking for a two-character language code, when typically four characters, like de-DE or en-US I could not quite make out how you are switching languages with your links, I believe there is another script at work. Anyways, I tested your script on my localhost by setting the language with thisdocument.documentElement.setAttribute('lang','de-DE');
and I changed your script a little:<script> jQuery(document).ready(function(){ if( document.documentElement.lang.toLowerCase() === "de-de" ) { langElements = getAllElementsWithAttributeValue('lang','en'); for (var i in langElements) { langElements[i].style.visibility = 'hidden'; langElements[i].style.display = 'none'; } } else { langElements = getAllElementsWithAttributeValue('lang','de'); for (var i in langElements) { langElements[i].style.visibility = 'hidden'; langElements[i].style.display = 'none'; } } }); function getAllElementsWithAttributeValue(attribute,attributevalue) { var matchingElements = []; var allElements = document.getElementsByTagName('*'); for (var i = 0, n = allElements.length; i < n; i++) { if (allElements[i].getAttribute(attribute) == attributevalue) { // Element exists with attribute. Add to array. matchingElements.push(allElements[i]); } } return matchingElements; } </script>
and I used your example HTML:
<span lang="en">Hello World!</span><span lang="de">Hallo Welt!</span>
and this worked for me, but on your site I can not change the language with the icons, so you could check your script for the two-character language code, or you could try this script instead.
Best regards,
MikeNovember 10, 2020 at 7:44 am #1259493Dear Mike,
Thanks a lot for your feedback and the information. Indeed, the language switcher is an extra-script that sets a cookie-value depending on which language-icon of the website was pressed. And then, the script in enfold’s “Google Analytics Tracking Code” area checks out whether “de” or “en” is set as cookie value and hides the respective content in the alternative language.
I’ve tried out your script and it still does not work. I think the problem is that the ready-function is not triggered.
To check, I tried out, e.g.:
<script> jQuery(document).ready(function(){ alert("Ready!"); }); </script>
which did not work; as an alternative, I tried out:
<script> window.addEventListener('load', function () { alert("Ready!"); }); </script>
which also did not work. Is there any possibility to trigger the ready-function (or the eventListener “load”)?
Thanks again and all the best,
ChristianNovember 10, 2020 at 1:06 pm #1259549Hi,
Please include the language switcher script so we can test it also.
I tested your two alert on load scripts above and both worked for me.Best regards,
MikeNovember 12, 2020 at 12:37 pm #1260099This reply has been marked as private.November 15, 2020 at 3:05 am #1260537Hi,
Sorry for the late reply and thanks for the login. I couldn’t get your script to work on my localhost. I did find that there is an issue with the Google Analytics theme option, this has been reported to the dev team.
Meanwhile, a better approach to adding your scripts would be in your functions.php file in Appearance > ThemeS > Editor
So for example your alert script would look like this:function custom_script(){ ?> <script> jQuery(document).ready(function(){ alert("Ready!"); }); </script> <?php } add_action('wp_footer', 'custom_script');
Please note that for each script you add you will need to change the
custom_script
part of the code to be unique, no two functions can have the same name.Best regards,
MikeNovember 17, 2020 at 1:13 am #1260860Dear Mike,
Thanks a lot for the information. That’s very helpful. I placed the code in the functions.php file and now everything works properly. Clearly, if the Google Analytics area would work out, this would be preferable, because changes in the functions.php file have to be restored manually after an update whereas changes in the Google Analytics area are kept also across updates. But that everything works now again is already really very helpful. Thanks again — very much appreciated.
All the best and take care,
ChristianNovember 17, 2020 at 1:41 am #1260866Hi,
Did you need additional help with this topic or shall we close?
Best regards,
Jordan ShannonNovember 17, 2020 at 1:48 am #1260867Hi,
Thanks a lot, I’m fine. I think we can close this topic.
Thanks again and all the best,
ChristianNovember 17, 2020 at 1:53 am #1260868Hi,
If you need additional help, please let us know here in the forums.
Best regards,
Jordan Shannon -
AuthorPosts
- The topic ‘Language Switcher JavaScript stopped working after Enfold update’ is closed to new replies.