在本地文件系统上的 HTML 中设置根目录的绝对路径 [英] Set absolute path for root directory in HTML on local filesystem

查看:73
本文介绍了在本地文件系统上的 HTML 中设置根目录的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的本地文件系统上进行测试时,如何在我的网站中使用绝对路径?我知道我可以使用 / 来访问我网站的根目录.但是,这仅在我的网站在服务器上时才有效.我希望能够在我的本地文件系统上使用绝对路径,以便我可以在上传之前进行适当的测试.

How can I use absolute paths in my website while testing on my local filesystem? I know that I can use / to access the root directory of my website. However, this works only when my website is on the server. I want to be able to use absolute paths on my local filesystem so that I can do proper testing before uploading.

有没有办法将变量设置为 HTML 中的根目录?类似于 Linux 的东西,您可以在其中定义一个变量 WEBPATH=/home/user/website.因此,我可以将例如 src="WEBPATH/folder/file.html 用于我在网站中使用的所有文件,并且我可以根据我是否在本地测试来修改 WEBPATH或使用服务器根文件夹.

Is there a way to set a variable to a root directory in HTML? Something similar to Linux where you can define a variable WEBPATH=/home/user/website. Thus I can use e.g src="WEBPATH/folder/file.html for all the files I use in my website and I can modify WEBPATH depending on whether I am testing locally or using the server root folder.

我也愿意接受其他解决方法.

I am open to other workarounds as well.

推荐答案

我假设您使用文件 url 来访问浏览器中的 HTML,在这种情况下,使用绝对路径的一种简单方法是使用为您的网站提供服务的本地网络服务器.

I'm assuming you're using a file url to access your HTML in the browser, in which case an easy way to get absolute paths working is by using a local webserver to serve your site.

如果你安装了 python 2.x,你可以在你的 web root 的命令行运行 python -m SimpleHTTPServer,它会在 localhost:8000 为你的站点提供服务代码>.

If you have python 2.x installed, you can run python -m SimpleHTTPServer from the command line at your web root, and it will serve your site at localhost:8000.

这篇关于在本地文件系统上的 HTML 中设置根目录的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆