473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

determine js filename from inside the file?

Hi,

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

Thanks,
Peter

Apr 9 '06 #1
6 1604
pe**********@gmail.com wrote:
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
Apr 9 '06 #2
VK

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>

Apr 9 '06 #3
On 8 Apr 2006 19:43:30 -0700, pe**********@gmail.com wrote:
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.
Apr 9 '06 #4
Jim Ley wrote:
On 8 Apr 2006 19:43:30 -0700, pe**********@gmail.com wrote:
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
Apr 9 '06 #5
On Sun, 09 Apr 2006 16:41:04 +0200, Thomas 'PointedEars' Lahn
<Po*********@web.de> wrote:
Jim Ley wrote:
On 8 Apr 2006 19:43:30 -0700, pe**********@gmail.com wrote:
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.
Apr 9 '06 #6
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.

Apr 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below...
18
by: Keith Brown | last post by:
I have an application that allows embedded storage of ANY chosen file in an OLE field. The file could have been dragged-and-dropped into the field or it might have been selected and imported...
5
by: Dino Buljubasic | last post by:
My application can allow a user to open a file for viewing by fetching file data from database, creating the file in a temp directory and starting appropriate process (i.e. Adobe or any other...
4
by: B Squared | last post by:
I'm trying to pass a filename (which is a jpeg image) to a php function / file so that it will display. I know that its simple to get PHP to display an image hardcoding in the filename. For...
3
by: Mark Gibson | last post by:
Is there an equivalent to the unix 'file' command? $ file min.txt min.txt: ASCII text $ file trunk trunk: directory $ file compliance.tgz compliance.tgz: gzip compressed data, from Unix ...
0
by: ABC | last post by:
Which function can determine the URI is web-based or File-based filename string? I want a function can determome the file with path is web or file-system-based filename, for example: ...
3
by: Phoe6 | last post by:
Hi all, I had a filesystem crash and when I retrieved the data back the files had random names without extension. I decided to write a script to determine the file extension and create a newfile...
3
beacon
by: beacon | last post by:
Hi, I'm doing a homework assignment, but I have done most of the work and am only looking for some tutoring to get past this small portion of the program I have been tasked to write. The...
3
by: LordHog | last post by:
Hello, How would I go about finding the default handler, let's say a text file (*.txt), then launch the default handler with the file as an argument? I had found how to launch an external...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.