Need help writing this mootools line as normal Javascript

L

laredotornado

Hi,

Does anyone know how to rewrite this line (using the mootools "$"
object) in terms of Javascript that doesn't depend on a JS framework?

$.Require([gaJsHost + 'google-analytics.com/
ga.js'],callback.bind(this))

Thanks for your help, - Dave
 
L

Lasse Reichstein Nielsen

laredotornado said:
Does anyone know how to rewrite this line (using the mootools "$"
object) in terms of Javascript that doesn't depend on a JS framework?

$.Require([gaJsHost + 'google-analytics.com/
ga.js'],callback.bind(this))

Thanks for your help, - Dave

I'm guessing, but only guessing since I don't know Mootools, that
this fetches a javascript file and evaluates it and then calls the
callback function.
If that's the case, then perhaps *something* like:

var self = this;
var request = new XMLHttpRequest(); // Not cross-browser safe.
request.onreadystatechange = function(e) {
if (request.readyState == 4 && request.status == 200) {
var indirectEval = eval;
var result = indirectEval(request.responseText);
callback.call(self, result);
}
}
request.open("GET", gaJsHost + 'google-analytics.com/ga.js', true);
request.send();

Notice: There are a lot of details needed if you want to use
XMLHttpRequest accross browsers (e.g., older IEs don't support it by
that name).

/L
 
S

Sean Kinsey

Hi,

Does anyone know how to rewrite this line (using the mootools "$"
object) in terms of Javascript that doesn't depend on a JS framework?

$.Require([gaJsHost + 'google-analytics.com/
ga.js'],callback.bind(this))

By JS framework, do you mean general frameworks, or also frameworks
that deal with loading script resources exclusively?

If you can allow the latter then you might want to check out this
[http://msdn.microsoft.com/en-us/scriptjunkie/ff943568.aspx] article,
it discusses two of the major ones.

Sean
 
A

Asen Bozhilov

Lasse said:
 request.open("GET", gaJsHost + 'google-analytics.com/ga.js', true);

I am guessing too, but first argument of `Require' says there can be N
file names and callback function will be executed after all files are
fully loaded. The problem with your approach are dependencies between
different files. Expected behaviour is the files to be loaded in order
as defined in array.

One possible approach is to be used XHR, buffer the responses from
requests than pass to `eval' string which contains responses by
requests in order as defined in array. That will safe many calls of
`eval' and will solve the dependency problem.
 

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

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top