简体   繁体   中英

How can I handle dynamic content in external javascript file?

I have a website in several languages and it happens that there are some visitors who for some reason are viewing a page in a different language than that of their browser settings. Therefore, if their browser language is one of those the site is translated into, I suggest they switch to their preferred language. I do this with a div containing a message at the top of the page.

According to your settings, we suggest you to view the content of this page in the following language: {language link}.
{Denied option with link}

The message is shown in the current language (I have all the translations of course), while {language link} is taken from the user's settings. The javascript code is generated Serverside with PHP and managed by Javascript in the HTML

<script> 
// instructions loaded, matching user conditions...
</script>

Is there any way to manage it in an external js file? How to do? Obviously, I'm not interested in preparing n*n combinations of files and uploading the one that matches the visitor's situation.

How to do? Thank you!

You can probably write particular JavaScript server side that assigns data to a global variable, in addition to your external script.

In the server-rendered page, it'd look something like:

<script>
  var userSettingsLanguageLink = '{language link}';
</script>

And then in your external script, something like:

  if (userSettingsLanguageLink !== getBrowserLanguage())
  //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM