PDA

View Full Version : Strings in strings...



???
06-17-2007, 04:22 PM
I know how you can use "s and 's for strings, and I discovered that you can use \"s so you can probably do \'s. But what if you had five strings? Like this:

Function("Function('Function(\"Function(\'alert("hi")))));

Except the problem with that is that the alert is "s again. That wont work. Is there any way to do it without a variable? Like:

var extraquote = "alert('hi')"
Function("Function('Function(\"Function(\'"+extraquote+"\')\")')");

I no you would never want to do that, but for something like that, with 5 quotes inside each other.

Trinithis
06-17-2007, 06:44 PM
Function("Function(\"Function(\\\"Function(\\\\\\\"alert(\\\\\\\\\\\\\\\"hi\\\\\\\\\\\\\\\")\\\\\\\")()\\\")()\")()")();

Never do this.

The ()'s on the ends of the functions executes it so you don't need to store it onto a variable [such as myVar] and then do myVar() to execute it. If I'm not mistaken (function(params){/*statments*/})(args) is called a function literal.

Try using "function" statements instead of the "Function" constructor.

Twey
06-17-2007, 08:36 PM
Function("Function(\"Function(\\\"Function(\\\\\\\"alert(\\\\\\\\\\\\\\\"hi\\\\\\\\\\\\\\\")\\\\\\\")()\\\")()\")()")();Hahaha, brilliant.

As with eval(), if you find yourself using the Function constructor in this way, there's probably a better way to do it. Converting strings to code is, in general, a Bad Thing. There are a few situations where it's OK (like as a JSON parser, for example), but they're few and far between.