// Create a function to change the hash
// value of the page and reload it
function changeLanguage(lang) {
location.hash = lang;
location.reload();
}
// Define the language reload anchors
var language = {
eng: {
welcome:
"Welcome to the GeeksforGeeks " +
"Portal! You can choose any " +
"language using the buttons above!",
},
es: {
welcome:
"¡Bienvenido al portal GeeksforGeeks! " +
"¡Puedes elegir cualquier idioma usando " +
"los botones de arriba!",
},
hin: {
welcome:
"GeeksforGeeks पोर्टल पर आपका स्वागत है! " +
"आप ऊपर दिए गए बटन का उपयोग करके किसी भी " +
"भाषा को चुन सकते हैं!",
},
};
// Check if a hash value exists in the URL
if (window.location.hash) {
// Set the content of the webpage
// depending on the hash value
if (window.location.hash == "#es") {
siteContent.textContent = language.es.welcome;
} else if (window.location.hash == "#hin") {
siteContent.textContent = language.hin.welcome;
}
}
HTML
GeeksforGeeks
Click on the buttons below to change
the language of the webpage.
示例:此示例通过显示三个按钮供用户选择任何语言,并在单击按钮时更改语言来演示上述方法。
HTML
GeeksforGeeks
Click on the buttons below to change
the language of the webpage.
Change to English<
/button>
Change to Spanish
Change to Hindi
Welcome to the GeeksforGeeks Portal!
You can choose any language using the
buttons above!