how to get url of script

D

deostroll

hi,

Suppose I have a script tag somewhere in my page as follows:

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

I would want to get the exact uri of the js page (something like
http://mysite.com/myscripts.js). Or at least get the name of the js
file (myscripts.js). Is this possible?

--deostroll
 
T

Thomas 'PointedEars' Lahn

deostroll said:
Suppose I have a script tag somewhere in my page as follows:

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

That is a SCRIPT/script _element_, consisting of _start_ and _end_ tag.

I would want to get the exact uri of the js page

There is no such thing as a "js page". (There are no "Web pages" either,
except in paged presentation such as a printout. The former is a _script_
written in an _ECMAScript-based/-conforming_ programming language
[commonly: JavaScript or JScript]; the latter is an _[X]HTML_ _document_.)
(something like http://mysite.com/myscripts.js). Or at least get the name
of the js file (myscripts.js). Is this possible?

Very likely nowadays. Yes, but not on the client side; the client can only
provide information about the URI of the resource.

I believe these questions have been answered before.

<http://jibbering.com/faq/#posting> pp.


PointedEars
 
S

Scott Sauyet

Suppose I have a script tag somewhere in my page as follows:

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

I would want to get the exact uri of the js page (something like
http://mysite.com/myscripts.js). Or at least get the name of the js
file (myscripts.js). Is this possible?

Yes you can get the URI, but there are some problems with differences
between IE and most other browsers.

There are several questions, though. First, where are you doing it
from? Are you trying to find the URI from within myscripts.js, or
from within another script? Second, if it's the latter, how do you
identify among the possibly numerous SCRIPT elements the one you
want? Can you give it an id?

In general, though,

var scripts = document.getElementsByTagName("SCRIPT");

will give you a list of the script tags. Then you have to find the
correct one. If you are doing this code within the linked script, you
*might* be able to use scripts[scripts.length - 1] to refer to the
current script element; I haven't tested this widely, though. I
believe that although the scripts can be downloaded in any order, they
are supposed to be evaluated in document order.

Once you have your script element, you can check it's "src"
attribute. Here's where IE often differs from the other browsers; it
might well return "myscript.js", while the others will likely return
"http://mysite.com/myscript.js". You will have to resolve this by
combining the short form with the document.location.href or -- if a
BASE element is present -- the href of the BASE element. This is not
trivial to do, but it's not too hard either. I'm sure there are
examples of this to be found on the Web.

Good luck,

-- Scott
 
R

Richard Cornford

On Mar 18, 3:28 pm, Scott Sauyet wrote:
... ? Second, if it's the latter, how do you
identify among the possibly numerous SCRIPT elements
the one you want? Can you give it an id?
<snip>

Since HTML SCRIPT elements are not specified as supporting an ID
attribute, giving one such an attribute may not be that useful an
action. There is certainly no reason for expecting it to be useful
beyond the observation that in some environments the element does
support that attribute.

Richard.
 
D

David Mark

Scott said:
Yes you can get the URI, but there are some problems with differences
between IE and most other browsers.

See here:-

http://www.cinsoft.net/attributes.html

....which demonstrates such inconsistencies for numerous attributes, as
well as a cross-browser workaround that makes virtually all of them act
alike.
There are several questions, though. First, where are you doing it
from? Are you trying to find the URI from within myscripts.js, or
from within another script?

If within, it would be well-advised to wait until the document is ready.
Second, if it's the latter, how do you
identify among the possibly numerous SCRIPT elements the one you
want? Can you give it an id?

In general, though,

var scripts = document.getElementsByTagName("SCRIPT");

will give you a list of the script tags.

A collection of references to the SCRIPT elements.
Then you have to find the
correct one. If you are doing this code within the linked script, you
*might* be able to use scripts[scripts.length - 1] to refer to the
current script element; I haven't tested this widely, though.

No, you really should wait until the DOM is ready (the information you
need may not be available until then).
I
believe that although the scripts can be downloaded in any order, they
are supposed to be evaluated in document order.

Not just supposed to, they are.
Once you have your script element, you can check it's "src"
attribute.

....with a wrapper like the above.
Here's where IE often differs from the other browsers; it
might well return "myscript.js",

It will in IE < 8 and IE8 compatibility mode.
while the others will likely return
"http://mysite.com/myscript.js".

Almost certainly. Also, it is useful to note that checking the SRC
property may yield this as well (which is also addressed by the wrapper).
You will have to resolve this by
combining the short form with the document.location.href or --

window.location.href or document.URL, but no need to check those to work
around the MSHTML attributes problem.
if a
BASE element is present -- the href of the BASE element.

....if the BASE element has an HREF attribute that is. But there's no
need to do that either.
This is not
trivial to do, but it's not too hard either. I'm sure there are
examples of this to be found on the Web.

You know right where one is. ;)
 
D

David Mark

David said:
See here:-

http://www.cinsoft.net/attributes.html

...which demonstrates such inconsistencies for numerous attributes, as
well as a cross-browser workaround that makes virtually all of them act
alike.

And looking at the original proposition, it is not an IE-specific
problem. Same example applies though.
There are several questions, though. First, where are you doing it
from? Are you trying to find the URI from within myscripts.js, or
from within another script?

If within, it would be well-advised to wait until the document is ready.
Second, if it's the latter, how do you
identify among the possibly numerous SCRIPT elements the one you
want? Can you give it an id?

In general, though,

var scripts = document.getElementsByTagName("SCRIPT");

will give you a list of the script tags.

A collection of references to the SCRIPT elements.
Then you have to find the
correct one. If you are doing this code within the linked script, you
*might* be able to use scripts[scripts.length - 1] to refer to the
current script element; I haven't tested this widely, though.

No, you really should wait until the DOM is ready (the information you
need may not be available until then).
I
believe that although the scripts can be downloaded in any order, they
are supposed to be evaluated in document order.

Not just supposed to, they are.
Once you have your script element, you can check it's "src"
attribute.

Its src _property_.
...with a wrapper like the above.


It will in IE < 8 and IE8 compatibility mode.

Correction, it is not the usual broken MSHTML issue at play here. Many
browsers foul this up (and the specs are ambiguous, so none can be
faulted with anything more than odd judgment). Basically, the standard
getAttribute returns the literal value, so the property _should_ return
the qualified URI (to allow scripts to retrieve either).
Almost certainly. Also, it is useful to note that checking the SRC
property may yield this as well (which is also addressed by the wrapper).

Most will return the qualified URI (referred to as the "resolved" URI in
the wrapper). And IIRC, IE is not among the offenders that return the
literal attribute value.
 
D

Dr J R Stockton

In comp.lang.javascript message <36e3ffff-8f6f-41c5-958d-436a5b8ef15b@z3
g2000yqz.googlegroups.com>, Thu, 18 Mar 2010 08:28:00, Scott Sauyet
In general, though,

var scripts = document.getElementsByTagName("SCRIPT");

will give you a list of the script tags. Then you have to find the
correct one. If you are doing this code within the linked script, you
*might* be able to use scripts[scripts.length - 1] to refer to the
current script element; I haven't tested this widely, though. I
believe that although the scripts can be downloaded in any order, they
are supposed to be evaluated in document order.

If it is necessary to be sure that the right script has been found, and
one has control over the content of the scripts, one can always label
the first statement of each script uniquely, and check for that. If
unused labels might be omitted, use a unique identifier or string.
 
E

Evertjan.

Dr J R Stockton wrote on 19 mrt 2010 in comp.lang.javascript:
In comp.lang.javascript message <36e3ffff-8f6f-41c5-958d- 436a5b8ef15b@z3
g2000yqz.googlegroups.com>, Thu, 18 Mar 2010 08:28:00, Scott Sauyet
In general, though,

var scripts = document.getElementsByTagName("SCRIPT");

will give you a list of the script tags. Then you have to find the
correct one. If you are doing this code within the linked script, you
*might* be able to use scripts[scripts.length - 1] to refer to the
current script element; I haven't tested this widely, though. I
believe that although the scripts can be downloaded in any order, they
are supposed to be evaluated in document order.

If it is necessary to be sure that the right script has been found, and
one has control over the content of the scripts, one can always label
the first statement of each script uniquely, and check for that. If
unused labels might be omitted, use a unique identifier or string.

Or you could use serverside Javascript coding to include the URL,
using header ContentType to make the file a clientside Javascript file:

============ myJs.asp =================
<%@LANGUAGE="JavaScript"%>
<%
// classic ASP Javascript code: start
Response.contenttype = 'text/javascript';
var urlVarServer = Request.servervariables("SERVER_NAME");
urlVarServer += Request.servervariables("URL");
// classic ASP Javascript code: end
%>

var myUrl = '<% = urlVarServer %>';

// your clientside Javascript code here
=======================================

========== myHtml.html ================
<script type="text/javascript" src="myJs.asp"></script>
<script type="text/javascript">
document.write('The url of the above js-script file: ' + myUrl);
</script>
=======================================
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top