469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

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 #1
21 17314
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Thu, 16 Dec 2004 10:54:43, seen in news:comp.lang.javascript,
ry******@yahoo.com 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
-------------
code to get the javascript.js file name?

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


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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #2
ry******@yahoo.com wrote:
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?


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
ry******@yahoo.com wrote:
[...]

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[i].src) {
msg += '\n' + m[i].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.

--
Rob
Jul 23 '05 #4
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.

Dr John Stockton wrote:
JRS: In article <11**********************@z14g2000cwz.googlegroups .com> , dated Thu, 16 Dec 2004 10:54:43, seen in news:comp.lang.javascript,
ry******@yahoo.com 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
-------------
code to get the javascript.js file name?

Is there a way to get the filename in which the code is being
executed?
Put var ThisIs = "javascript.js"
inside file javascript.js - or try rephrasing the question.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ

items, links.

Jul 23 '05 #5
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?

Jul 23 '05 #6
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.

Jul 23 '05 #7
<ry******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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>
Jul 23 '05 #8
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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #9
"Ryan Hubbard" <ry******@yahoo.com> wrote in message
news:41**********@127.0.0.1...
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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

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"?
Jul 23 '05 #10


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*****@agricoreunited.com>
comp.lang.javascript 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*****@agricoreunited.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.demon.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.idiotsdelight.net/minitrue/> Update hope?
Jul 23 '05 #13

First, the following is the current standard:
<script type='text/javascript' >src='somejavascript.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='somejavascript.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.demon.co.uk> wrote in message
news:18**************@merlyn.demon.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*****@agricoreunited.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*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #17
Grant Wagner wrote:
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:18**************@merlyn.demon.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*****@agricoreunited.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.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.

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

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

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

Jul 23 '05 #20

co********@yahoo.ca wrote:
Jay Den wrote:
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.
I apologize I should have stated where my statistics where taken from.
The rough stat I posted above was taken from the last six months
records from Nielsen//Netratings. If fortune 500 companies make
decisions based on this information it is very likely that the op can
as well.
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


Jul 23 '05 #21
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

Jay Den wrote:
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.

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

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

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 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

185 posts views Thread by jacob navia | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.