I have been developing a phaser game that will be integrated into a website through an iframe. The game has the capability to support multiple languages, and we have decided to determine the language based on the site from which the game was accessed (for example, phaser-game.com/ru would display in Russian, phaser-game.com/ar would display in Arabic, etc).
Here is the current code snippet triggered by
window.addEventListener('load', getDomainSetLanguage);
:
function getDomainSetLanguage()
{
let url = (window.location !== window.parent.location) ? document.referrer : document.location.href;
console.log('url = ' + url);
for (let i = 0; i < COUNTRIES_DOMAIN.length; i++)
{
if (url.indexOf(COUNTRIES_DOMAIN[i].URL) >= 0)
{
DOMAIN_ID = COUNTRIES_DOMAIN[i].ID;
LANGUAGE_ID = COUNTRIES_DOMAIN[i].LANGUAGE_ID;
break;
}
}
if (DOMAIN_ID === -1)
{
DOMAIN_ID = 1;
}
if (LANGUAGE_ID === -1)
{
LANGUAGE_ID = 1;
}
console.log('DOMAIN_ID = ' + DOMAIN_ID + "; LANGUAGE_ID = " + LANGUAGE_ID);
}
While this implementation works well initially, there is a challenge when the game triggers a reload intermittently. Upon return, the game acquires its own URL instead of inheriting the parent's or iframe's URL.
As a result, the game default language switches to English.
It is worth noting that this issue only arises in Chrome and Safari browsers, with FireFox functioning correctly.
Is there something crucial that I might be overlooking? Any suggestions for alternative approaches?
I attempted to log the values of document.referrer
and document.location.href
, but encountered browser errors related to permissions, leading the game to revert to English as the default language.
In my research from here, I discovered that Chrome (and possibly Safari) may not trigger the onload function for objects within the iframe. However, given that other functions tied to onload are working fine, I am unsure if this explanation applies to my situation.
One important restriction is that I am unable to modify the iframe directly, so any resolution must originate from within the game itself.
Thank you!