使用Javascript将子页面中的页面链接到具有相同结构子文件夹的另一个子域

时间:2015-01-09 20:33:54

标签: javascript http hyperlink

我正在尝试创建一个链接子域的按钮,但保留页面的相同路径。

点击它来自

sub1.maindomain.com/folder01/folder02/folder03/

sub2.maindomain.com/folder01/folder02/folder03 /

我想编写脚本,我希望将其添加到网站模板中。

有点生锈我的JS。

谢谢你能给我指点。

2 个答案:

答案 0 :(得分:0)

var loc=window.location.href;
var path=loc.substring(19);//flesh this out to be smarter for your use case
var newdomain = "sub2.maindomain.com";

var link=jQuery("#linkid").attr('href',newdomain+path);

答案 1 :(得分:0)

您可以使用location对象。

var path = location.pathname; //pathname will return the complete path of the uri without the host/domain.
var queryString = location.search.length > 0 ? location.search : ""; //this will retrieve the querystring if available.
var newLink = "sub2.maindomain.com" + path + queryString;

document.getElementById("link").href = newLink; //replace [link] with an id from your anchor element/button element.
相关问题