pe**********@gmail.com wrote:
Hi,
Is there a way to determine the name of a JavaScript file from inside
the very same file?
No, unless it is a single external file used on the page. The problem
is that the engine first combines all declared <script> blocks into one
source text and only then parses it. So on the stage your script "cames
to live" it doesn't know anymore what block came from where:- unless of
course the author took some mesures prior that, like unified naming
schemas, object wrappers etc.
For a single external file it is a trivia:
// foobar.js
function getJSFileName() {
var js = document.getElementsByTagName('SCRIPT');
for (var i=0; i<js.length; i++) {
alert(js[i].src || 'inline');
}
}
if ('undefined' != typeof window.addEventListener) {
window.addEventListener('load',getJSFileName,true) ;
}
else if ('undefined' != typeof window.attachEvent) {
window.attachEvent('onload',getJSFileName);
}
else {
/*NOP*/
}
// demo.html
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="foobar.js"></script>
</head>
<body>
</body>
</html>