我正在尝试创建一个链接子域的按钮,但保留页面的相同路径。
点击它来自
sub1.maindomain.com/folder01/folder02/folder03/
到
sub2.maindomain.com/folder01/folder02/folder03 /
我想编写脚本,我希望将其添加到网站模板中。
有点生锈我的JS。
谢谢你能给我指点。
答案 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.