[英]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.