SHARE
TWEET

Untitled

a guest Feb 18th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let langs = ['en', 'fr'];
  2. let lang = 'en';
  3. setLangStyles(lang);
  4.  
  5. function setStyles(styles) {
  6.     var elementId = '__lang_styles';
  7.     var element = document.getElementById(elementId);
  8.     if (element) {
  9.         element.remove();
  10.     }
  11.  
  12.     let style = document.createElement('style');
  13.     style.id = elementId;
  14.     style.type = 'text/css';
  15.  
  16.     if (style.styleSheet) {
  17.         style.styleSheet.cssText = styles;
  18.     } else {
  19.         style.appendChild(document.createTextNode(styles));
  20.     }
  21.     document.getElementsByTagName('head')[0].appendChild(style);
  22. }
  23.  
  24. function setLang(lang) {
  25.     setLangStyles(lang);
  26. }
  27.  
  28. function setLangStyles(lang) {
  29.     let styles = langs
  30.     .filter(function (l) {
  31.         return l != lang;
  32.     })
  33.     .map(function (l) {
  34.         return ':lang('+ l +') { display: none; }';
  35.     })
  36.     .join(' ');
  37.  
  38.     setStyles(styles);
  39. }
  40.    
  41. <a href="" hreflang="en" onclick="setLang('en'); return false">English</a>
  42. <a href="" hreflang="fr" onclick="setLang('fr'); return false">Deutsch</a>
  43.     <p lang='en'>Hi everyone!</p>
  44.     <p lang='fr'>Guten Tag!</p>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top