473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to refer to the current Javascript file

Hi,

If I have this include on my HTML page

<script type="text/javascript" src="scripts/myScript.js"></script>

How, from within "myScript.js" can I refer to the path of myself? In
other words, I would like to store in a variable the path WEBROOT/
scripts/myScript.js.

Hope this makes sense. Thanks, - Dave
Sep 16 '08 #1
3 1541
SAM
laredotornado a écrit :
Hi,

If I have this include on my HTML page

<script type="text/javascript" src="scripts/myScript.js"></script>

How, from within "myScript.js" can I refer to the path of myself? In
other words, I would like to store in a variable the path WEBROOT/
scripts/myScript.js.

Hope this makes sense. Thanks, - Dave
var myPath = document.getElementsByTagName('script')[0].src;

Sep 17 '08 #2
SAM wrote:
laredotornado a écrit :
>Hi,

If I have this include on my HTML page

<script type="text/javascript" src="scripts/myScript.js"></script>

How, from within "myScript.js" can I refer to the path of myself? In
other words, I would like to store in a variable the path WEBROOT/
scripts/myScript.js.

Hope this makes sense. Thanks, - Dave

var myPath = document.getElementsByTagName('script')[0].src;
Maybe:

var scripts = document.getElementsByTagName('script'),
thisScript = scripts[scripts.length-1].src || location.href;

But that would only work while the script was loading.

It would not work if the script inserted other scripts before that line
of code. It would not be reliable in the jquery.ready function, or
window.onload or any other function that was called after the script had
loaded.

Why do you want to do this?

Garrett
Sep 17 '08 #3
SAM
dhtml a écrit :
SAM wrote:
>laredotornado a écrit :
>>>
If I have this include on my HTML page

<script type="text/javascript" src="scripts/myScript.js"></script>

How, from within "myScript.js" can I refer to the path of myself? In
other words, I would like to store in a variable the path WEBROOT/
scripts/myScript.js.

var myPath = document.getElementsByTagName('script')[0].src;

Maybe:
with in first script's tag of my tested file
src="test.js"

and file 'test.js' with :
alert(document.getElementsByTagName('script')[0].src);

that works (in my Firefox) as attempted
var scripts = document.getElementsByTagName('script'),
thisScript = scripts[scripts.length-1].src || location.href;

But that would only work while the script was loading.
of corse !
it's why it has to be in 1st position

It would not work if the script inserted other scripts before that line
of code. It would not be reliable in the jquery.ready function, or
window.onload or any other function that was called after the script had
loaded.
??

window.onload = function() {
alert(document.getElementsByTagName('script')[0].src);
}

in next script of my tested file works too

and it does too with in the body :

<a href="javascript:alert(document.getElementsByTagNa me('script')[0].src)">
path</a>

Why do you want to do this?
Yes ! what to do with a such info ?
--
sm
Sep 17 '08 #4

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

Similar topics

1
3106
by: MK | last post by:
What would be the best platform-independent way to refer to the current (execute) path for a Python script? self.dirname = ??? This variable should contain directory name for the current path....
5
12622
by: - CT - | last post by:
Is it possible to use javascript to get a list of files from the current directory where the html page that contains the javascript is being called? The reason is that I want to find all ".jpg"...
16
4410
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
16
3459
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
1
1402
by: ad | last post by:
I place a Loading.aspx in the Util folder of my project, Util folder in in the root directory. I refer Loading.aspx in this way: ~/Util/Loading.aspx in Javascript, But it fail to find...
3
5278
by: laryten | last post by:
Hi, Is there a way to update the same web page instead of getting a new page each time we click the submit button? The simplest thing to do is to delete the current page (or go back to the...
2
2212
by: aloke.sarnobat | last post by:
Hello, I am new to this forum, so your help would be most appreciated. I am trying to create a javascript where, on clicking a button, I will be able to download the latest log file. But the...
8
5192
by: Mateusz Viste | last post by:
Hi, I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)... ...
0
7205
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,...
0
7349
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...
1
7008
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...
0
5594
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,...
0
4688
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...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
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 ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.