473,729 Members | 2,079 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 18240

co********@yaho o.ca wrote:
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.
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.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 #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.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 #22

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

Similar topics

185
7061
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
8928
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
8766
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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
9291
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...
1
9217
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8160
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...
1
3244
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
2698
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2171
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.