From: David Mark on
Scott Sauyet wrote:
> On Mar 18, 12:43 am, deostroll <deostr...(a)gmail.com> wrote:
>> 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.

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. ;)
From: David Mark on
David Mark wrote:
> Scott Sauyet wrote:
>> On Mar 18, 12:43 am, deostroll <deostr...(a)gmail.com> wrote:
>>> 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.
>
> 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.
>
>> 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.

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).

>
>> 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).

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.
From: Dr J R Stockton on
In comp.lang.javascript message <36e3ffff-8f6f-41c5-958d-436a5b8ef15b(a)z3
g2000yqz.googlegroups.com>, Thu, 18 Mar 2010 08:28:00, Scott Sauyet
<scott.sauyet(a)gmail.com> posted:
>
>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Evertjan. on
Dr J R Stockton wrote on 19 mrt 2010 in comp.lang.javascript:

> In comp.lang.javascript message <36e3ffff-8f6f-41c5-958d-
436a5b8ef15b(a)z3
> g2000yqz.googlegroups.com>, Thu, 18 Mar 2010 08:28:00, Scott Sauyet
> <scott.sauyet(a)gmail.com> posted:
>>
>>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>
=======================================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)