Yes. I wrote a simple test case to show what is going wrong.
Put this in a .js file:
console.log('Message from beyond!');
Then write a simple HTML with nothing in it and with the following
script (don't forget mootools :-) ):
var req = new Request({
url : 'printMessage.js',
async : false,
onSuccess : function (responseText) {
try {
var toRun = '(function () {';
toRun += 'try {';
toRun += responseText;
toRun += '} catch (e) {console.log(e);}';
toRun += '})();';
console.log('This should be logged before.');
eval(toRun);
console.log('This should be logged after');
} catch (e) {console.log(e);}
}
}).send();
The console will print 'Message from beyond!' twice, in the following
order:
Message from beyond! <- after loading
This should be logged before. <- onSuccess
Message from beyond! <- eval
This should be logged after <- finish onSuccess
I didn't set evalScript true or did nothing that could change the
default behaviour.
I'm using mootools 1.2.4 (latest download from site) and I downloaded
the source to check what may be going wrong. And here is what I found
out:
The content type of a .js file would always contain the word
javascript, in my Apache server it was "application/javascript" and in
Jetty, as I stated before, application/x-javascript.
In mootools, line 4030, I found this:
if (this.options.evalResponse || (/(ecma|java)script/).test
(this.getHeader('Content-type'))) return $exec(text);
>From what I understand, that OR operator should be replaced by an AND
operator, otherwise, even if evalResponse is false, it will execute
the second statement if content-type contains the javascript word.
Isn't that correct?
Thanks for the quick answer.
Best regards.
On Nov 9, 4:33 pm, Oskar Krawczyk <[email protected]> wrote:
> That is very odd, as both, evalScripts and evalResponse are set to
> false by default. Are you sure you're not setting either to true?
>
> On 9 Nov 2009, at 18:14, visola wrote:
>
>
>
> > I'm having a problem loading a javascript file using Request. My
> > scenario is something like this:
> > I want to load the file as text and then run it manually in a more
> > restricted context (inside a "(function () { // file here})();" for
> > example).
>
> > But when Request finishes loading the file, it automatically runs it.
> > So after that, when I call it manually, it will run twice. From what I
> > saw in the code, Request calls "processScripts" that will execute
> > javascript files (identified by the content header - in my case
> > "application/x-javascript") even if I set evalResponse to false (the
> > default option).
>
> > My workaround is to name my files something else (without the .js
> > extension), otherwise my server will send it with the javascript
> > header and MooTools will execute it after downloading it.
>
> > Is there any other workaround for this? Shouldn't evalResponse be true
> > by default and when needed, setting it to false would avoid executing
> > scripts after loading them?
>
> > Best regards.