f



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

Per subject line, thanks.
0
justaguy
11/5/2016 9:35:07 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

11 Replies
486 Views

Similar Articles

[PageSpeed] 32

On 05-11-16 22:35, justaguy wrote:
>
> Per subject line, thanks.
>

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


0
Luuk
11/6/2016 8:49:21 AM
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.
0
JJ
11/6/2016 1:31:43 PM
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.
0
justaguy
11/6/2016 2:42:14 PM
From server side. Thanks.
0
justaguy
11/6/2016 2:44:19 PM
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.


0
Luuk
11/6/2016 6:21:20 PM
On Sunday, November 6, 2016 at 1:21:49 PM UTC-5, Luuk wrote:
> 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.

ok, thanks.
0
justaguy
11/6/2016 8:33:50 PM
Luuk <luuk@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)
0
Evertjan
11/7/2016 8:48:39 AM
On Monday, November 7, 2016 at 3:48:44 AM UTC-5, Evertjan. wrote:
> Luuk <luuk@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>
> 
> 

I appreciate it though I'm not an ASP developer.

0
justaguy
11/7/2016 10:23:43 PM
justaguy <lichunshen84@gmail.com> wrote on 07 Nov 2016 in
comp.lang.javascript: 

> On Monday, November 7, 2016 at 3:48:44 AM UTC-5, Evertjan. wrote:
>> Luuk <luuk@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>
>> 
>> 
> 
> I appreciate it though I'm not an ASP developer.

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.


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
0
Evertjan
11/7/2016 10:51:45 PM
On Monday, November 7, 2016 at 5:52:12 PM UTC-5, Evertjan. wrote:
>  wrote on 07 Nov 2016 in
> comp.lang.javascript: 
> 
> > On Monday, November 7, 2016 at 3:48:44 AM UTC-5, Evertjan. wrote:
> >> Luuk <luuk@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>
> >> 
> >> 
> > 
> > I appreciate it though I'm not an ASP developer.
> 
> 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.
> 
> 

Ok.  

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

0
justaguy
11/8/2016 4:01:55 PM
justaguy <lichunshen84@gmail.com> wrote on 08 Nov 2016 in
comp.lang.javascript: 

> On Monday, November 7, 2016 at 5:52:12 PM UTC-5, Evertjan. wrote:
>>  wrote on 07 Nov 2016 in
>> comp.lang.javascript: 
>> 
>> > On Monday, November 7, 2016 at 3:48:44 AM UTC-5, Evertjan. wrote:
>> >> Luuk <luuk@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>
>> >> 
>> >> 
>> > 
>> > I appreciate it though I'm not an ASP developer.
>> 
>> 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.
>> 
> 
> 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.



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
0
Evertjan
11/8/2016 6:19:15 PM
Reply: