473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3496


Yes :

document.getEle mentsByTagName( '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.getEle mentById('somei d').src
Danny
Jun 25 '06 #2
Danny wrote:


Yes :

document.getEle mentsByTagName( '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.getEle mentById('somei d').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_tex t" 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="anothe rfunction();">' +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*******@mail inator.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_tex t" 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="anothe rfunction();">' +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?te xt=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.getEle mentsByTagName( "script");
var lastScript = scrs[scrs.length-1];
// extract vars after ?
var src = lastScript.src;
var paramstr = src.substring(s rc.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/rasterTriangleD OM.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
3057
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> <http://www.physci.org/test/003url/index.html?url=http://mybiz.com/&that=this&when=now#21> <http://www.physci.org/test/003url/index.html?url=http://mybiz.com/&when=now> Before I go offering it in public (and writing it into any number of the 'development kits'...
2
5363
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 code below in the Web.Config file defines the authentication as forms and the aspx file required for login if the user is unauthenticated... this works well for the root level company web pages however when a user viewing the website of SubCompanyA...
7
1787
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 names derived from that location. For example: lblBVimp, txtBVsignoff, txtBVsignoffdate, chkBV Now I know i can do a "For Each XXX in myForm.Controls", If XXX.name = blahblahblah,
6
1499
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. Thanks
2
3575
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 clicks on the "Browse" button on the "<input type="File"... control and selects a file located in one of many possible mapped drives. I want to save the location of the file as a path as you would see when mapping a drive. For instance,...
6
1645
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 file is here: www.website.com/web/01/file.html Is it possible with javascript to give the value "01" to the DIV's ID?
0
2700
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 to whether my solution is robust or whether there is a better way to do it. What I've been trying to do is to create a core project with different extension projects, one for each customer. I wanted to create a different Setup project for each...
2
2122
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 database is a bunch of locations to images, though instead of a full path it only includes the file name not an absolute path( e.g. image.jpg). Now im trying to copy the names to another database this is no problem but obviously the the images don't...
4
2960
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 picture successfully unto the server but not so with the file name of the picture even though other parameters in my form got inserted successfuly in the database. I have read several posts on this topic including here on this site but I can't get around...
0
9595
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
10603
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
10353
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
10356
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
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.