如何链接到其他域中的同一页面?

时间:2016-06-15 18:09:36

标签: html wildcard

我正在进行本地化项目,直到我们在网站abc.com上进行翻译,我们创建了网站def.com作为外语的镜像。

我们在abc.com上放置一个语言选择器,其中包含指向def.com的静态链接,但我希望用户在网站层次结构中被带到同一页面,而不是主页他们点击了。网站结构已完全镜像,因此我们不关心404。

要明确,我需要一种在链接点击上执行此操作的方法,我们不希望使用.htaccess或其他服务器端方法重定向。

提前致谢。

2 个答案:

答案 0 :(得分:0)

你可以做的一件事就是在Javascript中添加一个click事件,获取当前的URL,并将字符串修改为适当的域。

//get the URL in JS
var urlStr = window.location.href;

//where abc and def are your languages.
//Note you'll switch the order for the other site (You can also just do a simple check)
var newUrlStr = str.replace("abc", "def");

//redirect to new url
window.location = newUrlStr; 

这不是我所说的漂亮,但它会完成工作。

答案 1 :(得分:0)

Window.location.pathname将为您提供您所在的页面,不包括域名。 这样的事情应该有效:

document.getElementById("idOfMyLink").click(function(){
 window.location.href = "def.com" + window.location.pathname;
});

如果您还想包含查询字符串(?query = value)或哈希值(#anchor),您可以使用:

document.getElementById("idOfMyLink").click(function(){
 window.location.href = "def.com" + window.location.pathname  + "?" + window.location.search + "#" window.location.hash;
});