Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML中的image/css/javascript引用应该使用相对路径还是绝对路径?_Html_Path_Relative Path_Absolute Path - Fatal编程技术网

HTML中的image/css/javascript引用应该使用相对路径还是绝对路径?

HTML中的image/css/javascript引用应该使用相对路径还是绝对路径?,html,path,relative-path,absolute-path,Html,Path,Relative Path,Absolute Path,使用相对路径或绝对路径引用web资产的优缺点是什么?例如: <link rel="StyleSheet" href="/css/mystylesheet.css" type="text/css" /> <img src="/images/myimage.gif" alt="My Image" /> vs 答案是“视情况而定”。但大多数时候,绝对路径可能是最好的 我在使用模板的地方使用绝对路径,或者在使用Apache的mod_rewrite时使用绝对路径 如果您有一

使用相对路径或绝对路径引用web资产的优缺点是什么?例如:

<link rel="StyleSheet" href="/css/mystylesheet.css" type="text/css" />
<img src="/images/myimage.gif" alt="My Image" />

vs


答案是“视情况而定”。但大多数时候,绝对路径可能是最好的

我在使用模板的地方使用绝对路径,或者在使用Apache的mod_rewrite时使用绝对路径


如果您有一个带有样式表的页面,并且该页面在上传时可能被放置在不同的级别,那么您可以使用相对路径。i、 e.您已经编写了一个将在许多网站上使用的页面,有些人可能会将其上载到根目录,有些人可能不会-通过使用相对路径,只要他们同时上载html文件和css文件,它会起作用-而在这种情况下绝对路径不会起作用。

它取决于文件的服务器端组织。如果使用或,则相对路径可能不起作用


另一方面,如果您使用绝对路径(即使您只是使用“普通”HTML页面),您也可以重新排列页面,而不必考虑它们在您的结构中的位置。

如果您选择使用绝对路径而不是使用相对路径,最大的问题是可维护性

例如,想象一下,如果要更改服务器文件的结构,甚至要将应用程序移动到其他环境。要做到这一点,您将需要额外的工作,因为需要更改所有绝对路径。对于相同的场景,使用相对路径后,不再需要额外的工作


我的建议是在开发过程中使用相对路径作为标准,只有在绝对必要的情况下,才选择使用绝对路径。

我认为您使用相对路径和绝对路径的方式不正确。第一个例子是绝对的,第二个是相对的。
<link rel="StyleSheet" href="../css/mystylesheet.css" type="text/css" />
<img src="../images/myimage.gif" alt="My Image" />