Results 1 to 3 of 3

Thread: Strings in strings...

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Strings in strings...

    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.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    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.
    Last edited by Trinithis; 06-17-2007 at 06:57 PM.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •