2017-05-07 53 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')