Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button id='switchLink'>switch language</button>


              
            
!

CSS

              
                
              
            
!

JS

              
                /*
 * switch current url
 * based on current language folder
 * pure Vanilla JavaScript method 🧠 for modern browsers
*/
// function definition
function switchLanguage() {
  // get the switch button from the HTML markup
	const switchButton = document.getElementById('switchLink');
  // returns the button element via ES6 template strings
	console.log(`🍑 button id = ${switchButton.id}`);
  
	// get current host url
	let currentHost = window.location.host;
  // returns 'cdpn.io'
	console.log(`🍌 current host = ${currentHost}`);
  
  // create an array by breaking up the current url pathname by '/'
	let pathNameArray = window.location.pathname.split('/'); // will split: '/boomboom/index.html'
  // returns ["", "boomboom", "index.html"]
  console.log(pathNameArray);
  
  // access the second element of the splitted url array
	let currentLanguage = pathNameArray[1];
  // returns 'boomboom'
	console.log(`🍉 current language = ${currentLanguage}`);
  
  // access the third element of the splitted url array
	let currentHTMLpage = pathNameArray[2];
  // returns 'index.html'
	console.log(`🥑 current HTML page = ${currentHTMLpage}`);
  
	// add a 'click' event listener on the switch link
	switchButton.addEventListener('click', function() {
			// if current language equals 'fr'
			if (currentLanguage == 'fr') {
				// define new english URL
				let newLocation = window.location.protocol + '//' + currentHost + '/en/' + currentHTMLpage;
        // open newly defined URL within same window
        window.open(newLocation, '_self');
        // switch the button text to 'fr'
        switchButton.innerHTML = 'fr';
				// else if current language equals 'en'
			} else if (currentLanguage == 'en') {
				// define new french URL
				let newLocation = window.location.protocol + '//' + currentHost + '/fr/' + currentHTMLpage;
        // open newly defined URL within same window
        window.open(newLocation, '_self');
        // switch the button text to 'en'
        switchButton.innerHTML = 'en';
			}
		} // end of else if statement
	); // end of event listener
} // end of function definition
console.clear(); // clear previous logs
switchLanguage(); // calls the function

/////////////// even shorter version 🤘🏻 ///////////////

/*
 * Unnamed IIFE
 * with ES6 arrow function
*/
(() => {
	const switchButton = document.getElementById('switchLink');
  let protocol = window.location.protocol;
	let host = window.location.host;
	let pathNameArray = window.location.pathname.split('/');
	switchButton.addEventListener('click', function() {
			if (pathNameArray[1] == 'fr') {
        window.open(protocol + '//' + host + '/en/' + pathNameArray[2], '_self');
        switchButton.innerHTML = 'en';
			} else if (pathNameArray[1] == 'en') {
        window.open(protocol + '//' + host + '/fr/' + pathNameArray[2], '_self');
        switchButton.innerHTML = 'fr';
			}
		}
	);
})();


              
            
!
999px

Console