473,834 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1625
pe**********@gm ail.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**********@gm ail.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.getEle mentsByTagName( 'SCRIPT');
for (var i=0; i<js.length; i++) {
alert(js[i].src || 'inline');
}
}

if ('undefined' != typeof window.addEvent Listener) {
window.addEvent Listener('load' ,getJSFileName, true);
}
else if ('undefined' != typeof window.attachEv ent) {
window.attachEv ent('onload',ge tJSFileName);
}
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**********@gm ail.com wrote:
Is there a way to determine the name of a JavaScript file from inside
the very same file?


document.all[document.all.le ngth-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**********@gm ail.com wrote:
Is there a way to determine the name of a JavaScript file from inside
the very same file?


document.all[document.all.le ngth-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.getEle mentsByTagName( "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*********@we b.de> wrote:
Jim Ley wrote:
On 8 Apr 2006 19:43:30 -0700, pe**********@gm ail.com wrote:
Is there a way to determine the name of a JavaScript file from inside
the very same file?


document.all[document.all.le ngth-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.getEle mentsByTagName( "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
6804
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 by Norman Diamond the bug may result in loss of data when saving files with the standard common dialog control. Norman Diamond describes the problem as follows: ---------- In VB6 SP6, VB6 SP5, and possibly others, the common dialog box...
18
7724
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 programmatically using the common file dialog. Regardless, I need to determine the filetype/extension of each of these files already stored in my OLE fields and display it for the user. Double-clicking the raw OLE field or using the .Verb =...
5
4602
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 depending of file type) to open the file. when I close my application, it is suppose to clean all files it created in temp directory. I do this by : for each file in temp
4
2510
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 example, an href to this: <?php header("content-type:image/jpeg"); $filename = "image_file.jpg"; $im=ImageCreateFromJPEG($filename); ImageJPEG($im);
3
2439
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 What I really want to do is determine if a file is 1) a directory, 2) a
0
1207
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: http://abc.com/docs/123.txt return 'Web-Based'
3
9121
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 with extension. --- method 1: # File extension utility. import os
3
1731
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 program says that a filename of "numbers.txt" will be defaulted to if the user presses enter when prompted. I set this up as a string and had no problem getting the default value to work properly. My problem occurs when the user tries to create a file with...
3
2548
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 program, but I do not know how I would find the default handler to a file type. Any help is greatly appreciated. Code to launch application: ProcessStartInfo pInfo = new ProcessStartInfo();
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10782
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10213
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4422
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.