我想用子域制作翻译语言,假设我的原语是在印度尼西亚用www.domain.com制作的,我想用域名en.domain.com制作英文版本,因此当用户单击它时,页面将变为英语因此,我正在尝试使用JavaScript将子域名从“ www.domain.com/path/ ..”替换为“ en.domain.com/path/ ..”。我正在尝试编写此代码,但是它不起作用
<script type="text/javascript">
/* <![CDATA[ */
function doGTranslats() {location.href=location.protocol+'//'+location.hostname.replace('en')+location.pathname+location.search;}
/* ]]> */
</script>
<li><a href="#" onclick="doGTranslats();"hidefocus="true"><font>English</font></a></li>
任何想法如何解决此问题?
let hostParts = window.location.hostname.split('.');
hostParts[0] = 'en';
hostParts = hostParts.join('.')