如果我像这样加载JavaScript源文件:
<!DOCTYPE html>
<html>
<head>
<script src="src/file.js"></source>
</head>
<body>
</body>
</html>
我的问题是file.js
如何知道其位置的相对路径是src/
?我需要这些信息来构建源文件中的URL,该URL取决于从中加载的URL。
答案 0 :(得分:2)
<script src="src/file.js" id="script0"></script>
在file.js中:
var path = document.getElementById ( "script0" ).getAttribute ( "src" );
答案 1 :(得分:0)
如果您使用getAttribute
,您将获得书面的src。如果您使用.src
属性,则会获得带有当前位置的src。