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

F

Fred Lazy

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?
 
D

Danny

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
 
F

Fred Lazy

Danny said:
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...
 
L

Lasse Reichstein Nielsen

Fred Lazy said:
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.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
 
F

Fred Lazy

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top