'Weglot js call works on chrome but not on Safari - can't figure out why
I'm new to js and jQuery so please forgive me if this code seems a little lost. I'm trying to hide/unhide Webflow classes based on the Weglot language selection. The following code works perfectly in chrome, but usually fails on safari (however not on iOS - here it works fine). On safari(desktop) I usually get the "document ready" in the js console, but I hardly ever get the "Switcher ready!", which would indicated that Weglot is loaded and ready.
Can anyone see what I've done wrong?
//test doc
$(document).ready(function() {
console.log("document ready!");
//test weglot ready
Weglot.on("switchersReady", function() {
console.log("Switcher ready!");
//set language
var current = Weglot.getCurrentLang();
console.log("current lang is :" + current);
setLang(current);
$("a#weglot-language-en").click(function() {
var current = Weglot.getCurrentLang();
console.log("current lang is :" + current);
setLang(current);
});
$("a#weglot-language-da").click(function() {
var current = Weglot.getCurrentLang();
console.log("current lang is :" + current);
setLang(current);
});
$("a#weglot-language-sv").click(function() {
var current = Weglot.getCurrentLang();
console.log("current lang is :" + current);
setLang(current);
});
function setLang(sprog) {
switch (sprog) {
case "en":
$(".svendvideo.dansk").hide();
$(".svendvideo.svensk").hide();
$(".svendvideo.engelsk").show();
break;
case "da":
$(".svendvideo.dansk").show();
$(".svendvideo.svensk").hide();
$(".svendvideo.engelsk").hide();
break;
case "sv":
$(".svendvideo.dansk").hide();
$(".svendvideo.svensk").show();
$(".svendvideo.engelsk").hide();
break;
default:
// code block
}
}
});
});
Solution 1:[1]
If I understand correctly, you're trying to show/hide elements regarding the current language.
Have you maybe tried to work with the "initialized" function? I'm pleased to send you below the documentation regarding the "initialized" function. The callback function will be called only once Weglot will be initialized on your website. https://developers.weglot.com/javascript/javascript-functions#initialized
See an example here
Weglot.on("initialized", callbackFunction)
Otherwise, maybe could you in your function, only check if there is Weglot working on your website, by checking for instance:
if(Weglot){ /* do actions */ }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tripleee |