最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use window.location.href.replace to switch language in the href url? - Stack Overflow

programmeradmin2浏览0评论

I have a link that is used to switch languages in my site. When the user click on the link I want it to direct user to the chinese version of the page that is located inside the /ch/ folder.

For example, when user is on mysite/account/en/index.html, when click on the link, user should be redirected to mysite/account/ch/index.html

I tried the below code but it is not working. Anyone know a better code?

<li class="lang"><a id="langswitch" href="javascript: window.location.href.replace('/en/', '/ch/');" ><img src="/images/account/buttons/chinese.jpg" alt="chinese language" align="top" /></a></li>

I have a link that is used to switch languages in my site. When the user click on the link I want it to direct user to the chinese version of the page that is located inside the /ch/ folder.

For example, when user is on mysite./account/en/index.html, when click on the link, user should be redirected to mysite./account/ch/index.html

I tried the below code but it is not working. Anyone know a better code?

<li class="lang"><a id="langswitch" href="javascript: window.location.href.replace('/en/', '/ch/');" ><img src="/images/account/buttons/chinese.jpg" alt="chinese language" align="top" /></a></li>
Share Improve this question asked Feb 18, 2014 at 23:33 user3108698user3108698 7193 gold badges9 silver badges23 bronze badges 1
  • ch usually refers to Switzerland; use zh to refer to Chinese! see: en.wikipedia/wiki/IETF_language_tag and unicode/cldr/utility/languageid.jsp?a=zh&l=en – feeela Commented Feb 18, 2014 at 23:41
Add a ment  | 

2 Answers 2

Reset to default 3

I believe all that you are missing is

window.location = window.location.href.replace('/en/', '/ch/');

why cant you use

<li class="lang"><a id="langswitch" href="mysite./account/cn/index.html" ><img src="/images/account/buttons/chinese.jpg" alt="chinese language" align="top" /></a></li>
发布评论

评论列表(0)

  1. 暂无评论