Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's an efficient way to record the number of loading / calling of a JS script file?

15 views
Skip to first unread message

justaguy

unread,
Nov 5, 2016, 5:35:14 PM11/5/16
to

Per subject line, thanks.

Luuk

unread,
Nov 6, 2016, 3:49:27 AM11/6/16
to
On 05-11-16 22:35, justaguy wrote:
>
> Per subject line, thanks.
>

# grep test.js /var/log/apache2/www-access_log | wc -l
42


JJ

unread,
Nov 6, 2016, 8:31:52 AM11/6/16
to
On Sat, 5 Nov 2016 14:35:07 -0700 (PDT), justaguy wrote:
> Per subject line, thanks.

If you meant from the web browser or client-side JavaScript, monitor the
SCRIPT element's `load` and `error` events, and monitor the global object's
`error` event.

The SCRIPT element's `load` event will fire if the script code has been
loaded, parsed, and executed.

The SCRIPT element's `error` event will fire if the script code has failed
to be loaded.

The global object's `error` event will fire if a script code has an
unhandled error.

You'll need to have a count of the total number of external scripts either
at design time (i.e. server side), or at run time (i.e. client side). If
from client side, use the `document.scripts` to determine the number of
external scripts.

If you load the JavaScript files using SCRIPT elements as external scripts,
then you'll need to monitor the events using the `onload` and `onerror`
attributes of the SCRIPT elements because the resources may load very fast
especially if you have fast network or if you serve the files from a local
web server. If you monitor the events using the SCRIPT element object's
`onload`/`onerror` properties, or using `addEventListener()`, the events may
have already been fired before you have the chance to attach the event
listeners.

If you load the JavaScript files using XHR, then you'll have to also listen
to the XHR's `load` and `error` events, in order to determine whether a
JavaScript file is still loading or not.

justaguy

unread,
Nov 6, 2016, 9:42:27 AM11/6/16
to
I'm on windows os, so, probably we dont have grep command utility. The other thing is i've disabled web server log. Thanks tho.

justaguy

unread,
Nov 6, 2016, 9:44:24 AM11/6/16
to
From server side. Thanks.

Luuk

unread,
Nov 6, 2016, 1:21:49 PM11/6/16
to
On 06-11-16 15:42, justaguy wrote:
> I'm on windows os, so, probably we dont have grep command utility.
>

http://gnuwin32.sourceforge.net/packages/grep.htm
or
http://www.powergrep.com/

> The other thing is i've disabled web server log. Thanks tho.

The most efficient way to record how many time a client has initiated a
load for that script (which is my interpretation of your question), is
to enable server side logging.


justaguy

unread,
Nov 6, 2016, 3:33:56 PM11/6/16
to
ok, thanks.

Evertjan.

unread,
Nov 7, 2016, 3:48:44 AM11/7/16
to
Luuk <lu...@invalid.lan> wrote on 06 Nov 2016 in comp.lang.javascript:

>> The other thing is i've disabled web server log. Thanks tho.
>
> The most efficient way to record how many time a client has initiated a
> load for that script (which is my interpretation of your question), is
> to enable server side logging.

Which will also accomodate for not counting the possible clientside re-use
of a cashed version.

Using classic ASP:

// js-file content here, call this file "myJS.asp"
<%@ Language="JScript" %>
<%
var ForReading = 1, ForWriting = 2, count, f;
var filename = Server.MapPath("count-myJS-asp.txt");
var fso = new ActiveXObject("Scripting.FileSystemObject");

if (fso.FileExists(filename)) {
f = fso.OpenTextFile(filename, ForReading);
count = f.ReadAll();
f.Close();
} else {
var f = fso.CreateTextFile(filename, true);
f.Close();
count = 0;
};

f = fso.OpenTextFile(filename, ForWriting);
// count = -1; // reset counter to 0
f.Write(++count);
f.Close();
%>
================

Read the number into an html-file:

<iframe src = 'count-myJS-asp.txt'> </iframe>


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

justaguy

unread,
Nov 7, 2016, 5:23:54 PM11/7/16
to
I appreciate it though I'm not an ASP developer.

Evertjan.

unread,
Nov 7, 2016, 5:52:12 PM11/7/16
to
justaguy <lichun...@gmail.com> wrote on 07 Nov 2016 in
comp.lang.javascript:
ASP is fully developed, why would you want to develop it? [joke]

You can do exactly the same in PHP, [or in VBScript, btw]
but then, this is a Javascript NG,
so serverside JS is on topic,
while PHP is not.

justaguy

unread,
Nov 8, 2016, 11:02:05 AM11/8/16
to
Ok.

I have a new question titled "This question has two parts: 1) Level of Difficulty (2) Level of Elegance".

Evertjan.

unread,
Nov 8, 2016, 1:19:21 PM11/8/16
to
justaguy <lichun...@gmail.com> wrote on 08 Nov 2016 in
> I have a new question titled "This question has two parts: 1) Level of
> Difficulty (2) Level of Elegance".

No, you don't, meaning you should not.

You should research my code,
try to understand it,
then search for php equivalents,
and try to implement it there.

You are like a man begging for the right turn at every corner,
while holding a map and a streetguide under his arm,
afraid that looking at it would exhaust him.

And btw, I repeat, while serverside javascript is on topic here, php is not.
0 new messages