我試圖創建一個按鈕,它將鏈接一個子域,但保持相同的頁面路徑。使用Javascript將頁面從子域鏈接到具有相同結構子文件夾的另一個子域
點擊它從
sub1.maindomain.com/folder01/folder02/folder03/
到
sub2.maindomain.com/folder01/folder02/folder03/
我去想要編寫腳本,我希望將其添加到網站模板中。
有點生鏽與我的JS。
謝謝,如果你能給我指針。
我試圖創建一個按鈕,它將鏈接一個子域,但保持相同的頁面路徑。使用Javascript將頁面從子域鏈接到具有相同結構子文件夾的另一個子域
點擊它從
sub1.maindomain.com/folder01/folder02/folder03/
到
sub2.maindomain.com/folder01/folder02/folder03/
我去想要編寫腳本,我希望將其添加到網站模板中。
有點生鏽與我的JS。
謝謝,如果你能給我指針。
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);
您可以使用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.
請向OP(以及未來的訪問者)解釋爲什麼你做了你所做的事情。 – 2015-01-09 21:27:58