2

If I load a JavaScript source file like so:

<!DOCTYPE html>
<html>
    <head>
        <script src="src/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.

2 Answers 2

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

In file.js:

var path = document.getElementById ( "script0" ).getAttribute ( "src" );
Sign up to request clarification or add additional context in comments.

1 Comment

I just thought about it myself too :). I'll just get all script elements, loop through them and use a simple regex replace to take the relative path (if the src matched).
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.