簡體   English   中英

File://協議的絕對相對路徑

[英]relative path to absolute with file:// protocol

我在具有類似html的網站上抓取

<a href="/pages/1></a>

我也有window.location對象

origin:"http://www.example.org"

所以我可以建立絕對路徑,例如origin + href = http://www.example.org/pages/1

我在文件系統上制作了頁面的模型以進行測試。

-www.example.org
  |-2017
    |-pages
      |-1.html
      |-2.html
  |-2016
    |-pages
      |-1.html
      |-2.html

在這些html文件中,鏈接看起來像這樣:

<!-- www.example.org/2016/pages/1.html --> <a href="../../2017/pages/1.html">2017</a>

在測試中,相同的代碼將不起作用,因為window.location對象的來源是file://

hash:""
host:""
hostname:""
href:"file:///home/me/projects/fp/src/test/fixtures/www.example.org/2016/pages/1.html"
origin:"file://"
pathname:"/home/me/projects/fp/src/test/fixtures/www.example.org/2016/pages/1.html"
port:""
protocol:"file:"

產生origin + href = file://../../2017/pages/1.html。 如果協議是file: file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html則可以通過一些字符串操作從location.pathname創建file:///home/me/projects/fp/src/test/fixtures/www.example.org/2017/pages/1.html 但這是解決此問題的正確方法嗎?

file://僅可用於絕對路徑。

當前工作目錄僅適用於相對路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM