2017-05-07 52 views
2

作爲工作的一部分,相同的webapp在多個位置爲不同的單元託管保存與當前網站的相對路徑的書籤

例如,

http://site1.come/path-to-some-page 
http://site2.come/path-to-some-page 

現在我有一個site1的書籤保存爲

http://site1.come/path-to-some-page 

但對於站點2我將不得不重新創建一個新的書籤。我必須每週處理一個新的域名,每週都會一次又一次地完成這項任務是很痛苦的。

相對於當前主機,我可以不保存書籤嗎

例如,

http://{CURRENT_HOST}/path-to-some-page 

這將節省很多我的書籤保存爲每一個新網站

回答

2

我還沒有看到一個方法來保存相對或根目錄相對鏈接,而不訴諸一個書籤的痛苦。

至於一個書籤而言,這是比較容易產生一個,將帶你到任何路徑你想:

javascript:(rel=>{location=rel.startsWith('/')?`${location.protocol}//${location.host}${rel}`:`${location.protocol}//${location.host}${location.pathname}/${rel}`})('/path') 

末替換'path'用正確轉義包含任何路徑字符串你想。請注意,這將根據它們是否以/字符開頭來區分相對路徑和相對路徑。

在長形:

(rel => { 
    location = 
    // if the relative path starts with/
    rel.startsWith('/') 
     // go to http(s)://{domain}/{relative path} 
     ? `${location.protocol}//${location.host}${rel}` 
     // otherwise go to http(s)://{domain}/{current path}/{relative path} 
     : `${location.protocol}//${location.host}${location.pathname}/${rel}` 
// call the function providing the relative path to use 
})('/path')