有没有办法创建一个包含部分当前url的标记链接?
我会用它来改变语言,但停留在当前页面上!
如果我在:
www.mysite.com/contact
然后我按下:
<a>FR</a>
生成的链接将是:
www.mystie.com/fr/contact
亲切问候
试试这个:
HTML:
<a href="fr/contact" id="myLink">FR</a>
现在从javascript 获取url
$("#myLink").click(function(e){
e.preventDefault();
var url = document.url; // get current page url
url = url.substr(0, url.indexOf('/')); // get substring from url
url += "/" + $(this).attr("href"); // add href of a tag to url
window.location.href = url; // locate the page to url
});
您可以尝试使用等
function changeLng(lang) {
// remove language '/fr/' or '/es/' if exists
var path = window.location.pathname.replace(/'/[a-z]{2}/, '');
// reload the page same url with the lang prefix
window.location.href = '/' + lang + path;
}
链路可以是
<a href="javascript:changeLng('fr');">FR</a>
<a href="javascript:changeLng('es');">ES</a>
<a href="javascript:changeLng('en');">EN</a>
这会将任何url转换为特定于语言的示例
/contact
至/fr/contact
/about
至/fr/about
更新
去除郎的部分是简单的
function removeLng() {
// remove language
var path = window.location.pathname.replace(/'/[a-z]{2}/, '');
window.location.href = path;
}