I've been having a play around with Mootools 1.3 server-side today. Its really nice took some bashing to get it Classic ASP friendly but still full of goodness (tm).
While showing a friend what Mootools can do I came across a strange ASP related error that I've not seen before.
Microsoft JScript runtime error '800a138f'
'Response.WriteBlock(...)' is null or not an object
The code that caused it is one of Mootools funky "fo开发者_开发技巧rEach" helpers:
[1,2,3].each( function( i ) {
Response.Write( "<b>" + i + "</b>, " );
});
It really does not like this code at all. I commented out the Response.Write and had an empty block and it still threw the error. Very odd. I know ASP does some optimisation magic to group html blocks and asp blocks together (hence the Reponse.WriteBlock comment) but not sure why this would apply here.
This code will run however:
var fruits = [];
fruits.push( "apples" );
fruits.push( "oranges" );
fruits.each( function( fruit ) {
Response.Write( "<b>" + fruit + "</b>, " );
});
// As will this
var nums = [ 1,2,3,4,5 ];
nums.each( function(i) {
Response.Write( "<b>" + i + "</b>" );
});
Has anyone got any ideas what is happening to cause this error? I'll keep digging but wondered if any fellow JScript guys could shed some light. Might just be a ASP quirk?
[Update]
Wierdness continues, managed to get it to work but it seems to depend on what comes before the each loop. So to recap this does NOT work:
<%
[ 1,2,3,4,5 ].each( function(i) {
Response.Write( "<b>" + i + "</b>" );
});
%>
But this does (notice just the addition of the var line above it):
<%
var something = 1;
[ 1,2,3,4,5 ].each( function(i) {
Response.Write( "<b>" + i + "</b>" );
});
%>
Very odd indeed. Must be something to do with the way ASP is treating the blocks, maybe there is a rule in the parser about blocks starting with an (what it would see as rather random) array statement or at least its not be programmed to expect me to do that (I don't think they imagined server side mootools way back when)! Would still like an indepth explanation though so I can better avoid it.
Try changing your code to this…
<%
;[ 1,2,3,4,5 ].each( function(i) {
Response.Write( "<b>" + i + "</b>" );
});
%>
This is just a hunch.
In general it's considered bad practice to begin a line or especially a file with a (
or [
without its own semicolon. The main reason has to do with concatenation...
e.g.
//foo.js
1 + 1 == 2
//bar.js
("lol, string")
//foo.js + bar.js
1 + 1 == 2("lol, string") // TypeError: number is not a function
Since bar.js
has no way to know what could eventually be included before it, it should always include its own ;
before the (
i.e.
//bar.js
;("lol, string")
//foo.js + bar.js
1 + 1 == 2;("lol, string") // SUCCESS!!1!
My hunch is that something weird like that is going on in ASP land.
Since we don't have access to the source code of JScript or the ASP framework itself there's no way to be sure what is going on.
i have never seen response.writeblock in classic asp,
shouldnt it be response.write ?
精彩评论