【问题标题】:How to change Current URL using JS when a button clicked ( for changing language)单击按钮时如何使用 JS 更改当前 URL(用于更改语言)
【发布时间】:2021-10-22 19:42:22
【问题描述】:

假设我当前的 URL 是: http://arshohag.com/en/home

我想把它改成: http://arshohag.com/bn/home

我如何使用 JS 来做到这一点??

【问题讨论】:

标签: javascript url


【解决方案1】:

只是为了给 Lazy Coder 的答案添加一点...

html:

<a href='bn' id='button'>Change Language to BN</a>

Javascript:

var button = document.body.getElementById('button'); // assuming there is a button with the id='button';

button.addEventListener('click', function(e){
   e.preventDefault();//to stop link from processing.
   var lang = button.href; // gets language from link
   window.location = 'http://arshohag.com/'+lang+'/home';
}

【讨论】:

  • 我是初学者......只是找不到方法............谢谢你的想法......它很有帮助
【解决方案2】:

在你的 JS 点击事件中尝试这样的事情

  window.location = 'http://arshohag.com/bn/home'

【讨论】:

    【解决方案3】:

    你可以简单地用这一行来改变它:

    window.location.pathname = "/bn/home";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2019-06-29
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多