469,342 Members | 5,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

javascript filename

Hi.

Does anybody know how can JavaScript get the filename of the file
it's
located in?

For example, file 'mike.js' contains several functions and
variables. I would like to set the variable to the filename of this
file dynamically:

var sourceFileName = "mike.js";


Thank you in advance,
Mike


***************************

MichaelGershman.com

Life Is Life...

***************************

Aug 9 '05 #1
5 7589

<mg******@hotmail.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
Hi.

Does anybody know how can JavaScript get the filename of the file
it's
located in?

For example, file 'mike.js' contains several functions and
variables. I would like to set the variable to the filename of this
file dynamically:

var sourceFileName = "mike.js";


Once loaded from a .js file, a script becomes a part of the document that loaded it, just as if the script had been
coded directly into the document at the position of the <script src=...> tag.
After loading the script from an external file, no further reference is made to that file or its contents.
Yes - really.

--
Stephen Chalmers
Aug 9 '05 #2
mg******@hotmail.com wrote:
Hi.

Does anybody know how can JavaScript get the filename of the file
it's
located in?
You could try removing script elements with src attributes one at a time
then call the function. If the call fails you could guess that the
failure was because you removed that particular element. But it would
not be very pretty or robust I expect.

For example, file 'mike.js' contains several functions and
variables. I would like to set the variable to the filename of this
file dynamically:

var sourceFileName = "mike.js";


You can create new script elements and give them a src attribute that
contains the URI of a script file:

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'mike.js';
document.body.appendChild( oScript );
--
Rob
Aug 10 '05 #3
mg******@hotmail.com wrote:
Does anybody know how can JavaScript get the filename of the file
it's located in?


I didn't try this, but...
script elements live under the documentElement element, regardless of
where they're defined in the document. So you might think to fish them
all out with
getElementsByTagName. Furthermore, script elements are created at the
time they are encountered.

Therefore, you could place code at the top of your .js file to iterate
through all the current script elements and take the last one
encountered. Then check the .src on that element:

var aScripts;
aScripts=document.documentElement.getElementsByTag Name('script');
var lastIdx;
for (var idx in aScripts) lastIdx=idx;
alert ("I'm in " + aScripts[idx].src);

Hope it helps,
Csaba Gabor from Vienna

Aug 10 '05 #4
Dear,

Using oScript.src may be a great solution to solve it. But, in my
experience, HTML Document in IE will download it asynchronously. So, if
you set oScript.src and evaluate the variables or functions
immediately, you will get an undefined exception.

Because my js just runs in IE. So, I use an ActiveX Object (XMLHTTP
object) to download the source code. After I get the source code, I
append it to oScript.text. In this way, IE will load these source code
immediately, and you can use them right now.

ex:

var objHTTP = new ActiveXObject("Msxml2.XMLHTTP.4.0");
objHTTP.open("GET", filename, false);
objHTTP.send();
if(objHTTP.readyState == 4)
{
oScript.text=oScript.text + objHTTP.responseText;
}

var oObj=eval(function name);
RobG 寫道:
mg******@hotmail.com wrote:
Hi.

Does anybody know how can JavaScript get the filename of the file
it's
located in?


You could try removing script elements with src attributes one at a time
then call the function. If the call fails you could guess that the
failure was because you removed that particular element. But it would
not be very pretty or robust I expect.

For example, file 'mike.js' contains several functions and
variables. I would like to set the variable to the filename of this
file dynamically:

var sourceFileName = "mike.js";


You can create new script elements and give them a src attribute that
contains the URI of a script file:

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'mike.js';
document.body.appendChild( oScript );


--
Rob


Aug 10 '05 #5
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Tue, 9 Aug 2005 11:22:29, seen in news:comp.lang.javascript,
mg******@hotmail.com <mg******@hotmail.com> posted :

Does anybody know how can JavaScript get the filename of the file
it's
located in?

For example, file 'mike.js' contains several functions and
variables. I would like to set the variable to the filename of this
file dynamically:

var sourceFileName = "mike.js";


Include that statement in that file, preferably at the beginning.

If you vary file-names in an arbitrary fashion, write a simple tool on
your editing system that updates such lines at need.

If one Web page calls more than one *.js file, the variable will hold
the name of the most recently invoked file.
Alternatively : the name of the *.js file is given by being included in
an HTML "statement" such as
<script type="text/javascript" src="include1.js"></script> .

Rather than writing that all literally in HTML, write it with javascript
using a variable for the file name.

<script type="text/javascript">
St = "include1.js"
document.writeln(
'<script type="text/javascript" src="', St, '"><\/script>')
</script>

Test :
<script type="text/javascript">
document.write(LZ.toString())
</script>

That, for me, writes the code of LZ (which is in include1.js), showing
that a file whose name is given by St has been included.

--
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.
Aug 10 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Edo van der Zouwen | last post: by
3 posts views Thread by annon | last post: by
12 posts views Thread by Sharad Gupta | last post: by
2 posts views Thread by NewUser | last post: by
8 posts views Thread by chrisdude911 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.