473,698 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the Javascript file name from a function inside the file.

I know the subject might be confusing. I am no beginner with
javascript but I haven't been able to figure out how to get the
javascript file name from code inside the file. So you have an HTML
doc with script tag who's source is a javascript file.

<HTML>
<script src="javascript .js"></script>
</HTML>

Javascript.js
-------------
code to get the javascript.js file name?

Is there a way to get the filename in which the code is being executed?
Thanks

Jul 23 '05
21 18229


I should change something from my last post. It should not be at runtime
but rather it should be able to determine the file name inside a
function in that file is when it is called. Furthmore the attempt,
naturally, is to make this as cross browser compatible as possible
(except for Netscape 4.x cause that browser should just never had been
created.)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #11
"Ryan Hubbard" <ry******@yahoo .com> wrote in message
news:41******** **@127.0.0.1...


I should change something from my last post. It should not be at runtime but rather it should be able to determine the file name inside a
function in that file is when it is called. Furthmore the attempt,
naturally, is to make this as cross browser compatible as possible
(except for Netscape 4.x cause that browser should just never had been
created.)


Then it's simple:

Inside file1.js you'd have:

function1() {
var thisFile = 'file1.js';
// ...
}
function2() {
var thisFile = 'file1.js';
// ...
}
function3() {
var thisFile = 'file1.js';
// ...
}

Now every function in the file knows the filename.

The bottom line is there is simply no way to do this. The client-side
JavaScript has no way to know what file it exists in, at run or any
other time.

As for Netscape 4. So I guess IE 3 and 4 and Opera 4, 5, and 6 should
never have been created either because they don't support functionality
modern browsers support. How about Mozilla 1.0.1? Should it have never
been created? It's newer than Netscape 4, but it lacks some
functionality and contains bugs which makes writing scripts that support
both it and modern versions of Mozilla problematic.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #12
JRS: In article <W3************ *@news2.mts.net >, dated Fri, 17 Dec 2004
17:55:02, seen in news:comp.lang. javascript, Grant Wagner
<gw*****@agrico reunited.com> posted :

The bottom line is there is simply no way to do this. The client-side
JavaScript has no way to know what file it exists in, at run or any
other time.


When the file is constructed, the constructing process will be aware of
the file name. The process may be manual, or automated.

It is merely necessary to use a constructing stage that copies the file
name from the outside of the file to the inside of the file. Details
depend on the system being used.
If DOS batch is available, consider a batch file containing

copy draft.js %1.js
mtr -x+ -n %1.js ItsMe.* = "ItsMe='%1. js'"

partly tested; mtr is MiniTrue. That should convert (the only) line
from var ItsMe=''
to var ItsMe='filename .js'

I assume the file is not subject to arbitrary subsequent renaming.
A different approach would be to compute the line
<script src="javascript .js"></script>
in which case code should know its contents.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/> Update hope?
Jul 23 '05 #13

First, the following is the current standard:
<script type='text/javascript' >src='somejavas cript.js'></script> I am aware of the standards and thank you for pointing them out anyway
but this post is about something different.

Second, the following is not a true statement:
"At runtime any code in this javascript.js file will be
executed."
It may declare functions that may or may not be executed. Instead of depating semantics and getting really technical about an
issue that the post is not about how about we address the issue at hand.
Once again I am not a beginner.
Third, what's wrong with JRS' suggestion of just placing
a variable in the
file that identifies the name of the file?

Once again as in one of the above statements. This is a templating
system. The point is to Automate retreiving the file name. If we hard
coded it it would not be automated would it.

The point is if we call a function which resides in a javascript file.
Can that function determine what file it came from. I really can't
state it more clearly then that. The point is to automate this so hard
coding variables are out. The code must retrieve the file name through
the javascript API or core functions. It must be as cross browser as
possible.

Thanks in advance for anyone that can even say if it is possible.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #14
Ryan,

You don't seem to be having much luck finding an answer to your
original question. Perhaps if you were to explain why you need to do
this you might get some suggestions for alternatives.

Regards,
Tim.
"Ryan Hubbard" <ry******@yahoo .com> wrote in message
news:41******** **@127.0.0.1...

First, the following is the current standard:
<script type='text/javascript' >src='somejavas cript.js'></script>

I am aware of the standards and thank you for pointing them out
anyway
but this post is about something different.

Second, the following is not a true statement:
"At runtime any code in this javascript.js file will be
executed."
It may declare functions that may or may not be executed.

Instead of depating semantics and getting really technical about an
issue that the post is not about how about we address the issue at
hand.
Once again I am not a beginner.
Third, what's wrong with JRS' suggestion of just placing
a variable in the
file that identifies the name of the file?

Once again as in one of the above statements. This is a templating
system. The point is to Automate retreiving the file name. If we
hard
coded it it would not be automated would it.

The point is if we call a function which resides in a javascript
file.
Can that function determine what file it came from. I really can't
state it more clearly then that. The point is to automate this so
hard
coding variables are out. The code must retrieve the file name
through
the javascript API or core functions. It must be as cross browser as
possible.

Thanks in advance for anyone that can even say if it is possible.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 23 '05 #15
Lee
Ryan Hubbard said:
The point is if we call a function which resides in a javascript file.
Can that function determine what file it came from.


You cannot call a function that resides in a javascript file.
That's not just a semantical quibble. It's central to your problem.

At the time the page was loading, the function source was loaded from a
particular javascript file, but at the time that the function executes, that
relationship no longer exists. It is simply one of many functions loaded in the
current page.

In some browsers you may be able to search the contents of scripts that were
loaded to find the source for a function of the same name, but that doesn't even
ensure that you've found the source for the function that's executing.

Jul 23 '05 #16
"Dr John Stockton" <sp**@merlyn.de mon.co.uk> wrote in message
news:18******** ******@merlyn.d emon.co.uk...
JRS: In article <W3************ *@news2.mts.net >, dated Fri, 17 Dec 2004 17:55:02, seen in news:comp.lang. javascript, Grant Wagner
<gw*****@agrico reunited.com> posted :

The bottom line is there is simply no way to do this. The client-side
JavaScript has no way to know what file it exists in, at run or any
other time.
When the file is constructed, the constructing process will be aware

of the file name. The process may be manual, or automated.

It is merely necessary to use a constructing stage that copies the file name from the outside of the file to the inside of the file. Details
depend on the system being used.
If DOS batch is available, consider a batch file containing

copy draft.js %1.js
mtr -x+ -n %1.js ItsMe.* = "ItsMe='%1. js'"

partly tested; mtr is MiniTrue. That should convert (the only) line
from var ItsMe=''
to var ItsMe='filename .js'

I assume the file is not subject to arbitrary subsequent renaming.
Sure, the constructing process knows the name of the file. The
client-side JavaScript contained in the file does not know the name of
the file it resides in.

As you have pointed out, this information could be included in the
client-side JavaScript in a way that would make the client-side
JavaScript "aware" of the name of the file it resides in.
A different approach would be to compute the line
<script src="javascript .js"></script>
in which case code should know its contents.


Without external/server-side processing of some kind, I do not see how
client-side JavaScript can "know" what file it resides in. I don't
understand how your suggestion could be implemented using only
client-side technologies.

The OP has rejected the idea of inserting a variable assignment into
each file that contains the name of the file the variable assignment is
contained in. So either he does not understand that this process could
be automated by an external/server-side process, or perhaps he wants a
solution independant of server-side processing.

If he does not understand the process could be automated using
external/server-side processes, then perhaps he is not up to the
challenge, or he is simply uninformed. Either way I'm not going to do
his work for him.

If he demands a client-side only solution, the simple answer is as
already stated. There isn't one.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #17
Grant Wagner wrote:
"Dr John Stockton" <sp**@merlyn.de mon.co.uk> wrote in message
news:18******** ******@merlyn.d emon.co.uk...
JRS: In article <W3************ *@news2.mts.net >, dated Fri, 17 Dec 2004
17:55:02, seen in news:comp.lang. javascript, Grant Wagner
<gw*****@agrico reunited.com> posted :

The bottom line is there is simply no way to do this. The client-sideJavaScript has no way to know what file it exists in, at run or anyother time.

<...> A different approach would be to compute the line
<script src="javascript .js"></script>
in which case code should know its contents.


Without external/server-side processing of some kind, I do not see

how client-side JavaScript can "know" what file it resides in. I don't
understand how your suggestion could be implemented using only
client-side technologies.

The OP has rejected the idea of inserting a variable assignment into
each file that contains the name of the file the variable assignment is contained in. So either he does not understand that this process could be automated by an external/server-side process, or perhaps he wants a solution independant of server-side processing.

If he does not understand the process could be automated using
external/server-side processes, then perhaps he is not up to the
challenge, or he is simply uninformed. Either way I'm not going to do
his work for him.

If he demands a client-side only solution, the simple answer is as
already stated. There isn't one.


Hmmm --- that seems a bit categorical. It's not necessarily so, in
principle.

Regards,

../rh

Jul 23 '05 #18
This will work with any browser with the document.script s object and the
document.getEle mentsByTagName. This is but is not limited to Mozilla
1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the currect
browsers used on the net according to currect publishings.

The document.script s object loads a script object before the file is
included. Therefore any code which will execute at runtime can find
it's name. Use that to set the variable.

---- Code inside JS file -----------------------------
var scripts = document.getEle mentsByTagName( "SCRIPT")
var script = scripts[scripts.length-1].src

// Now depending on the browser the src could be
// just the file or the absolute URL so just parse it
var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)

// Now script Name contains the name of the script

------------------------------------------------------

The issue is, which nobody has brought up, is namespace. The above
automates retrieving the name which a function can then access but
multiple files using this code will overwrite the scriptName variable.
Each of my js files is a class (obviously each class is uniquely named).
I store the script name in a static class variable. This works perfect
for me.

Hope this helps.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #19
Jay Den wrote:
This will work with any browser with the document.script s object and the document.getEle mentsByTagName. This is but is not limited to Mozilla
1.0+, Opera 6+ , IE 5+, Netsape 6+. This covers over 95% of the currect browsers used on the net according to currect publishings.
[Unfortunately, since you appear to not consistently follow newgroup
protocol (i.e., provide attribution/quoted text), I don't know whether
this is simply your musings on the topic, or whether this is directed
to my earlier post.]

There are relatively frequent warnings in this newsgroup regarding
lies, damned lies, and the above. The methodology used to collect
browser demographics is usually highly flawed.
The document.script s object loads a script object before the file is
included. Therefore any code which will execute at runtime can find
it's name. Use that to set the variable.

---- Code inside JS file -----------------------------
var scripts = document.getEle mentsByTagName( "SCRIPT")
var script = scripts[scripts.length-1].src

// Now depending on the browser the src could be
// just the file or the absolute URL so just parse it
var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/)

// Now script Name contains the name of the script

------------------------------------------------------
It sounds like you've taken an empirical approach which may or may not
work reliably.

The issue is, which nobody has brought up, is namespace. The above
automates retrieving the name which a function can then access but
multiple files using this code will overwrite the scriptName variable. Each of my js files is a class (obviously each class is uniquely named). I store the script name in a static class variable. This works perfect for me.

Since you appear to be looking for a universal solution, perhaps Grant
is right that there isn't one.

If you wish to use something that may work in a number of browsers
(e.g., Firefox and Mozilla cousins, and Opera), consider loading the
library components dynamically and, with some care, monitor the changes
that occur in the global object as each loading occurs. That should
provide you with a relationship between the file being loaded, and the
global functions contained within.
Hope this helps.

Likewise.

Regards,
../rh

Jul 23 '05 #20

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

Similar topics

185
7029
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will return the file name given a FILE *. Questions: What would be the best name for this function?
0
8674
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
9027
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
8861
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...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
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();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.