开发者

Retrieving JavaScript source file location?

开发者 https://www.devze.com 2023-02-25 22:40 出处:网络
If I load a JavaScript source file like so: <!DOCTYPE html> <html> <head> <script src=\"src/开发者_StackOverflow社区file.js\"></source>

If I load a JavaScript source file like so:

<!DOCTYPE html>
<html>
    <head>
        <script src="src/开发者_StackOverflow社区file.js"></source>
    </head>
    <body>
    </body>
</html>

My question is that how could file.js know that the relative path to its location is src/? I need this information for building up a URL from the source file that depends on the URL it was loaded from.


<script src="src/file.js" id="script0"></script>

In file.js:

var path = document.getElementById ( "script0" ).getAttribute ( "src" );


If you use getAttribute you'll get the written src. If you use the .src attribute, you'll get the src with current location.

http://jsbin.com/igixi5/5/

0

精彩评论

暂无评论...
验证码 换一张
取 消