返回域取决于用户语言



如果用户语言浏览器设置为波兰语,任务是用javaScript/jQuery(也可以使用其他技术(编写一个脚本,以返回带有.pl扩展名的域。否则,脚本应返回.eu域扩展

我尝试使用jQuery,但找不到合适的解决方案。

<script type="text/javascript">
$(document).ready(function () {
			
var userLang = navigator.language || navigator.userLanguage;
			var path = window.location.path;
			var extension = window.location.hostname;
			var ext = extension.split(".");
			var x = ext[2];
			
if (userLang.startsWith("pl")) {
				    x = "pl";
window.location.href = extension + x + path;
else {
				x = "eu"
window.location.href = extension + x + path;
}
});
			
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果navigator.language="pl"else href=www.domain.eu/path ,我希望www.domain.pl/file1/files2/file3.html(链接可能有很多目录(

提前感谢您的任何贡献。

我找到了解决方案,也许对某人有用。此外,可以使用三元运算符代替if.else语句,以使代码更短、更可读。享受-感谢您的贡献。向社区致以问候。

<script type="text/javascript">
function myFunction(){
var userLang = navigator.language || navigator.userLanguage;

if (userLang.startsWith("pl")) {
var url = window.location.toString();
window.location = url.replace(".eu", ".pl");}
else {
url = window.location.toString();
window.location = url.replace(".pl", ".eu");
}
};
</script>

最新更新