Resig experimented with an idea a while back for jquery. One of the
issues that was hard for newbies to grasp was the need to wrap DOM
code in some kind of load event.
<script type="text/lazyload">
// code goes here
</script>
This code will not be executed, when in need of the script you just
search the Dom and eval. Resig evaluated these blocks after the load
elliminating the need for the code to wrapped in an event.
It's important to note that this only works with inline scripts.
On Nov 11, 2009, at 8:23 AM, Aaron Newton <[email protected]> wrote:
You could subclass Request and write your own custom handler for it.
Changing this in the library would probably break a bunch of sites.
To subclass:
var Request.JavaScript = new Class({
Extends: Request,
processScripts: function(text){
if (this.options.evalResponse && (/(ecma|java)script/).test
(this.getHeader('Content-type'))) return $exec(text);
return text.stripScripts(this.options.evalScripts);
}
});
On Wed, Nov 11, 2009 at 2:48 AM, visola <[email protected]>
wrote:
So the only way to not evaluate a javascript file (.js extension) is
changing its extension to something else so the default behaviour of
the server won't affect my code in the client?
Shouldn't that be optional? Or the scenario I'm working on is
completely unreal?
Aren't there times where one would like to download a javascript to
execute it later or in some other scope/context?
On Nov 10, 1:33 pm, Aaron Newton <[email protected]> wrote:
> Here's what the docs say:
>
> evalResponse - (*boolean*: defaults to false) If set to true, the
entire
>
> > response will be evaluated. Responses with javascript content-
type will be
> > evaluated automatically.
>
> So this is working properly.
>
> I think the intent here is to have the ability to return
javascript to
> request. If your headers are set properly, it evaluates it, but if
your
> headers are not set properly, and you can't easily change it, you
can force
> it to evaluate it.
>
> On Tue, Nov 10, 2009 at 2:57 AM, visola <[email protected]>
wrote:
>
> > 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.