473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Get name or location of .js file - is it possible?

Is is possible to retrieve the name of an .js file from within the .js file
itself:

For example, if the file is named "bla.js" and it is imported into an html
document:
<script type="text/javascript" src="bla.js"></script>

Is it then possible from within "bla.js" get the name of the .js file
similarly to the window.location property, but for the .js file itself and
not the window the .js file is contained in. In other words, does the
imported .js file have a location property, and if so, how can I access it?
Jun 25 '06 #1
4 3479


Yes :

document.getElementsByTagName('script')[0].src

bear in mind that, [0], refers to the 1st <script....> ... </script> element in the page. you
could also address it by means of id

<script .... src="somepath/something.js" id="someid">
document.getElementById('someid').src
Danny
Jun 25 '06 #2
Danny wrote:


Yes :

document.getElementsByTagName('script')[0].src

bear in mind that, [0], refers to the 1st <script....> ...
</script> element in the page. you
could also address it by means of id

<script .... src="somepath/something.js" id="someid">
document.getElementById('someid').src
Danny

Thank your for this information. Now I'm a little clearer about what I need,
as such, I wonder if the following may be possible?:

First of all, a simple script include tag would exist:

<script src="http://somedomain.com/something.js" id="Click here"></script>

Can the "id" be converted into a variable string within something.js and
later become a "linked_text" in the document.write html-output, as follows:

/* start (of an oversimplied version) of something.js */

var linked_text = somehow suck in the "id" of this include file;

d.write('<a href="..." onclick="anotherfunction();">'+linked_text+'</a>')

/* end of file */

Naturally the text_link variable could easily be passed via a standard
on-page javascript function above the include call, but the usage of the
script will be cross-domain, for site visitors to include a link to a
function on their websites, and be able to specify the linked text string,
but they cannot modify the external something.js file.

These site visitors are not necessarily half-knowledgable in js or html
either, so the shorter the code of the include, the better, i.e. one
include would be better than having one function containing only a variable
plus the include itself, i.e. I'm trying to find a one-liner solution.

Furthermore, would it be possible to re-use the include on the same html
page several times, with possibly different or sometimes even the same "id"
or linked_text, or would the function name conflict? On second thoughts,
maybe doing it the "id" way holds more trouble ahead than other methods...

Jun 25 '06 #3
Fred Lazy <fr*******@mailinator.com> writes:
First of all, a simple script include tag would exist:

<script src="http://somedomain.com/something.js" id="Click here"></script>
An id attribute value must not contain spaces, but more importantly,
the script element does not have an id attribute in HTML. Relying on
non-standard attributes is fragile, since browsers are bound to treat
them differently.
Can the "id" be converted into a variable string within something.js and
later become a "linked_text" in the document.write html-output, as follows:

/* start (of an oversimplied version) of something.js */

var linked_text = somehow suck in the "id" of this include file;

d.write('<a href="..." onclick="anotherfunction();">'+linked_text+'</a>')

/* end of file */
The only attribute of the script element that's safe to use is the
"src" attrbiute. The other valid attributes ("charset", "type",
"defer", and "language" - the last one only in transitional documents)
are meaningfull to the browser, and writing extra information into
them can make the script unusable.

So, if your user *really* can't be expected to make a script previous
to including yours where they set the necessary variables, e.g.,

<script type="text/javascript">
var something_text = "Click here";
var something_other = 42;
</script>

(which is pretty easy to describe to them and make examples of),
then I'd suggest using the src attribute:

<script src="somepath/something.js?text=Click%20here"></script>

Then you could use server-side scripting to create a script file
with the values pre-inserted (easier on your users).

If server-side scripting isn't possible, you'll have to fall back
on parsing the src attribute manually:
---
function extratVars() {
// find last script element on page
var scrs = document.getElementsByTagName("script");
var lastScript = scrs[scrs.length-1];
// extract vars after ?
var src = lastScript.src;
var paramstr = src.substring(src.indexOf("?")+1);
var params = paramstr.split("&");
var vars = {};
for (var i = 0; i < params.length; i++) {
var parts = params[i].split("=");
vars[unescape(parts[0])] = unescape(parts[1]);
}
return vars;
}
// use vars
var vars = extractVars();
alert(vars["text"]); // alerts "Click here"
---
Tested i Firefox, IE 6 and Opera 9.
Naturally the text_link variable could easily be passed via a standard
on-page javascript function above the include call, but the usage of the
script will be cross-domain, for site visitors to include a link to a
function on their websites, and be able to specify the linked text string,
but they cannot modify the external something.js file.
It's not that hard to explain that they need to set some variables
before including your script.

Or make a page for them where they can enter the text, and it generates
the code they need to insert.
These site visitors are not necessarily half-knowledgable in js or html
either, so the shorter the code of the include, the better, i.e. one
include would be better than having one function containing only a variable
plus the include itself, i.e. I'm trying to find a one-liner solution.


Use the "src", Luke :) If your goal is to make it simple for others
to use, you should use server side scripting and not rely on the browser
being able to work on the script element.

Or again, make a helper page where they write the texts in input
controls and then it generates the HTML they need to add to their
page.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 25 '06 #4
Lasse Reichstein Nielsen wrote:

[...]
Or again, make a helper page where they write the texts in input
controls and then it generates the HTML they need to add to their
page.


Thanks for switching the floodlight on the entire subject including the
various uses of the src attribute etc. A helper-page and server-side
generated cut-and-paste script the way it should ideally appear would be
the optimal solution. I will however go for the easy-to-implement
solution - simply being a variable above the js-include, but will reserve
the more advanced idea for a special purpose where the additional
development time and effort is justified. Altogether excellent stuff!
Jun 27 '06 #5

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

Similar topics

11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
2
by: Murphy | last post by:
Our website contains subdirectories for each subsidiary company, each company has it's own look and feel to the pages in their subdirectory although they are all part of the main website. The...
7
by: BradC | last post by:
(VB.NET 2002, Windows app). I'm going to be provided a two-letter string like "BV" or "TP" that represents a location. I then need to perform some actions on several form controls that have...
6
by: Just Me | last post by:
My Task List contains the following: At least one reference is missing the 'Name' attribute. Any suggestion on how toe find which project is missing it? Solution contains 25 projects. ...
2
by: Ben | last post by:
Hi, Thanks for reading this post... I am trying to get the computer/server names from the mapped drives on the Clients PC when they select a file from the upload "browse" button. When the user...
6
by: Luca | last post by:
Hi everyone, I'm super-newbie here... so thanx in advance for any help I shoud give a "value" to an DIV's ID that change following the name of the directory where the file is. For example the...
0
by: =?Utf-8?B?U2ltb25EZXY=?= | last post by:
Hi All I would like to install the same Windows Service project on the same server under different names, one for each customer. I have been able to do it but I would like an expert opinion as...
2
by: chazzy69 | last post by:
This is a little hard to explain but i will try to make it as clear as possible. Firstly here's the setup, 2 databases on 2 different servers (databases are identical) now within the first...
4
by: liberty1 | last post by:
Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help. PROBLEM NUMBER 1: Using PHP and MySQL, I am able to upload...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.