determine js filename from inside the file?

P

petermichaux

Hi,

Is there a way to determine the name of a JavaScript file from inside
the very same file?

Thanks,
Peter
 
T

Thomas 'PointedEars' Lahn

Is there a way to determine the name of a JavaScript file from inside
the very same file?

No, I don't think so. Especially, on the Web there are no files.


PointedEars
 
J

Jim Ley

Is there a way to determine the name of a JavaScript file from inside
the very same file?

document.all[document.all.length-1].src generally works for scripts
executed during loading in a progressive manner...

Jim.
 
T

Thomas 'PointedEars' Lahn

Jim said:
Is there a way to determine the name of a JavaScript file from inside
the very same file?

document.all[document.all.length-1].src generally works for scripts
executed during loading in a progressive manner...

True, and other DOMs may provide the means with other host objects,
too (W3C DOM: var scripts = document.getElementsByTagName("script"),
scripts[scripts.length - 1]). However, that is _not_ determining
the filename, it is determining the URL of the script resource.


PointedEars
 
J

Jim Ley

Jim said:
Is there a way to determine the name of a JavaScript file from inside
the very same file?

document.all[document.all.length-1].src generally works for scripts
executed during loading in a progressive manner...

True, and other DOMs may provide the means with other host objects,
too (W3C DOM: var scripts = document.getElementsByTagName("script"),
scripts[scripts.length - 1]). However, that is _not_ determining
the filename, it is determining the URL of the script resource.

Of course it is, from a file html document where the only place
scripts are loaded from files. in http loaded it will be the URL
certainly.

Jim.
 
D

ddailey

One could have a script in a .js file attached to an HTML document find
its filename.
It could find the <script> tags on the HTML page, and find the src of
those. If there is only one ,js file then the .js file would know that
that must be itself, and then it would know its filename. If there are
more than one, I suppose you'd have to incrementally open each and
parse to see if the functions inside them are identical to its own. If
you had two
<script src="f1.js"> and <script src="f2.js">
in a page and f1.js was a duplicate of f2.js neither would be able to
disambiguate which of the twins it was, I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top