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

R

ryanmhuc

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
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 16 Dec 2004 10:54:43, seen in (e-mail address removed) posted :
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

Put var ThisIs = "javascript.js"
inside file javascript.js - or try rephrasing the question.
 
R

Randy Webb

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

IE and Opera support the document.scripts collection and you can query
its src property. Mozilla won't give it that easy.

In testing locally, IE gives just the filename, Opera gives the full path.
 
R

RobG

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

In addition to the methods above, put this in a form:

<input type="button" value="click" onclick="
var m,
foundOne = false,
msg = '';
if (!(m = document.scripts))
m = document.getElementsByTagName('script');
for (var i=0; i<m.length; i++) {
if(m.src) {
msg += '\n' + m.src;
foundOne = true;
}
}
(foundOne)? alert(msg):alert('No script files');
">


Lightly tested in Firefox and IE, only works if either the scripts
collection or getElementByTagName is supported. You may want to extend
it further for other cases.
 
R

ryanmhuc

Thanks for the idea but this is going to be for a templating class
system and the document name will be changing constantly. I would like
to try to automate the file name retrieval so it does not have to be
manually inputted.
JRS: In article
, dated Thu, 16 Dec 2004 10:54:43, seen in (e-mail address removed) posted : executed?

Put var ThisIs = "javascript.js"
inside file javascript.js - or try rephrasing the question.
items, links.
 
R

ryanmhuc

Thanks for the idea but I fail to see how the document.scripts object
can give me the name of the script that code is running in? The script
object contains an array of sciprt objects. Each script object has a
src property which has the script name but if there is more then 1
script file loaded how would you identify which script object you code
is running under - Given that your code does not know what the file
name is from an manually inputted variable?
 
R

ryanmhuc

Thanks for the idea Rob but again how can you retrieve the proper
script object from the code being executed. The idea is automate the
retrieval of the script file name from code executing with the file.
So if you could retrieve the file name from the executing code to
compare it to the src property of a script object in the scripts
collection then you wouldn't need to iterate through the scripts
collection in the first place.
 
M

McKirahan

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

It's not clear to me what you want.

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

You want to get the name of the JavaScript "include" file when?

Also, what if the following is the case?

<HTML>
<script src="javascript1.js"></script>
<script src="javascript2.js"></script>
</HTML>
 
M

McKirahan

Ryan Hubbard said:
Perhaps my first post was not clear so hopefully this should clear it
up.

You have a javascript file that is included via the script tag in an
HTML file like so.
<SCRIPT language='javascript' src='somejavascript.js'></script>

At runtime any code in this javascript.js file will be executed. Is
there a way to programmatically via the code in the somejavascript.js
file to determine the javascript file name it came from. So the code in
the somejavascript.js file should determine that it is within the
somejavascript.js file. I know you could just code it in but where
attempting to automate this process hence the term programmatically.
Hope this clears up any confusion.


First, the following is the current standard:

<script type='text/javascript' src='somejavascript.js'></script>

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.

Third, what's wrong with JRS' suggestion of just placing a variable in the
file that identifies the name of the file?

Fourth, could you be more clear about "where [sic] attempting to automate
this process"?
 
G

Grant Wagner

Ryan Hubbard said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 17 Dec 2004
17:55:02, seen in Grant Wagner
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.
 
T

Tim Williams

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.
 
L

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.
 
G

Grant Wagner

Dr John Stockton said:
JRS: In article <[email protected]>, dated Fri, 17 Dec 2004
17:55:02, seen in Grant Wagner


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.
 
C

codeHanger

Grant said:
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
 
C

codeHanger

Jay said:
This will work with any browser with the document.scripts object and the
document.getElementsByTagName. 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.scripts 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.getElementsByTagName("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
 
R

ryanmhuc

Jay,
Thanks for the idea. I tested it out and it works great. It worked
in Opera 6 and 7, Netscape 6 and up, IE 5 and 6 and testing using
Mozilla 1.0 and 1.7. That is good enough for me. Thanks again for the
idea I was starting to think that it was not possible.

Ryan
 

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

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top