问题
Recently I was trying to get a quick alert box from javascript using mshta but I noticed something strange and I have no ideea what the problem is. This is,in a way,what I was trying to achieve:
mshta javascript:alert("The file was stored here:\"C:\\folder_with_space_ _.txt");
The error it gives is the one in the title of this post(char 57).I tried a combination of things and:
//code that works:
mshta javascript:alert("The file was stored here:\"sdadasd");
mshta javascript:alert("The file was stored here:\"\" sdadasd");
//error-notice the space;error on char 35
mshta javascript:alert("The file was stored here:\" sdasds");
It looks like it's giving error when the number of double-quotes is odd,but:
//error
mshta javascript:alert("The file was stored here:\" \"sdadasd");
I tried to do the same in a browser console and it worked. I believe is some kind of parser-error.How can I fix it?(I am thinking of using fromCharCode to directly insert the double quote).
Note: the commands were run from cmd.
回答1:
I'll start off with the version of the command that I got to work, and I'll explain why it works:
mshta "javascript:alert('The file was stored here:\x22C:\\folder_with_space_ _.txt');"
The first and perhaps most important point is that we are passing a single argument to mshta.exe (the JavaScript command to execute), so we should surround that entire argument in double quotes. This prevents the space from being treated as an argument delimiter.
The second point is that there doesn't seem to be a way to have double quotes inside the actual JavaScript commands. According to the question Escaping Double Quotes in Batch Script, there is no standard for escaping double quotes inside double quotes for cmd. Apparently, mshta.exe doesn't honor ""
or \"
(or at least, I couldn't get them to work). I suggest following Teemu's suggestion in the comments and use single quotes only for string delimiter in the JavaScript code. If, inside a string, you want to include a double quote character, use the hex literal \x22
.
来源:https://stackoverflow.com/questions/45132747/unterminated-string-constant-mshtajavascript