views:

189

answers:

3

When I run javascript script file in windows command line environment, and there is a free text coming after my code. How can I stop javascript interpreter to run into it? For example:

var fso = new ActiveXObject("Scripting.FileSystemObject");
delete fso;
exit(); // some kind of WORKING exit command
Hungry lazy frog ate a big brown fox.
+2  A: 

There is nothing you can do to stop the interpreter before it the compiler sees a particular line because the whole Javascript source file is first compiled to bytecode and it is the bytecode that is interpreted not your source code.

What you could do (though it would still be messy) would be to put some free text in a comment at the end of the file. Then you could open the source file and read it from the rest of the code. It still can't be completely free text though as it would have to be a valid comment

/*
 whatever text you want provided it doesn't contain * followed immediately by /
 */

Much better is simply to admit defeat and store any data you need in a separate file.

Duncan
+1  A: 

Even if you did exit, you'd still have a problem because the whole script block needs to be parsed before the first instruction is executed. With any bogus content following an exit instruction, the script would fail to parse at all, even though that code would never be reached.

If the free text was a single line, you could get away with a trailing //. Doesn't work for multi-line comments though, as you would need a closing */. Same for conditional-comments requiring /*@end @*/.

bobince
+2  A: 

If you surround that with quotes it will be interpreted/compiled but will have no effect eg

var fso = new ActiveXObject("Scripting.FileSystemObject");
delete fso;
exit(); // some kind of WORKING exit command
"Hungry lazy frog ate a big brown fox.";
James Westgate