Set absolute path for root directory in HTML on local filesystem

China☆狼群 提交于 2021-01-28 18:56:57

问题


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.

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.


回答1:


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.

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.



来源:https://stackoverflow.com/questions/32260798/set-absolute-path-for-root-directory-in-html-on-local-filesystem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!