Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Anyone want to guess what this does in javascript?

Anyone want to guess what this does in javascript?

Scheduled Pinned Locked Moved The Lounge
javascripttutorialquestion
20 Posts 12 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M dHatter
    wrote on last edited by
    #1

    String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

    var s = 'alert("hello!");'

    s.code();

    "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

    modified on Sunday, May 15, 2011 12:54 PM

    D A A D realJSOPR 10 Replies Last reply
    0
    • M M dHatter

      String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

      var s = 'alert("hello!");'

      s.code();

      "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

      modified on Sunday, May 15, 2011 12:54 PM

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      At a guess, it creates a new method 'code' on the object s, that when called sets up a recursive loop to itself? Ok, it creates a prototype method that when called from a string object reads the string and converts string value to a function that executes and displays a "Hello" message box. just tried it...... :)

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


      Folding Stats: Team CodeProject

      modified on Sunday, May 15, 2011 7:22 AM

      M 1 Reply Last reply
      0
      • M M dHatter

        String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

        var s = 'alert("hello!");'

        s.code();

        "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

        modified on Sunday, May 15, 2011 12:54 PM

        A Offline
        A Offline
        Abu Mami
        wrote on last edited by
        #3

        Barf?

        1 Reply Last reply
        0
        • M M dHatter

          String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

          var s = 'alert("hello!");'

          s.code();

          "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

          modified on Sunday, May 15, 2011 12:54 PM

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Is this a test?

          The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

          M 1 Reply Last reply
          0
          • M M dHatter

            String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

            var s = 'alert("hello!");'

            s.code();

            "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

            modified on Sunday, May 15, 2011 12:54 PM

            D Offline
            D Offline
            Daniel Scott
            wrote on last edited by
            #5

            Seems to me that it's just going to alert "hello!" once and that's it, but this is probably some kind of trick-question..

            M 1 Reply Last reply
            0
            • M M dHatter

              String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

              var s = 'alert("hello!");'

              s.code();

              "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

              modified on Sunday, May 15, 2011 12:54 PM

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              It gives you a valid reason to avoid javascript whenever possible.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              1 Reply Last reply
              0
              • M M dHatter

                String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

                var s = 'alert("hello!");'

                s.code();

                "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                modified on Sunday, May 15, 2011 12:54 PM

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Thank you very much. We now need an article and some techniques to protect against JavaScript Injection Attacks. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                M A 2 Replies Last reply
                0
                • A Abhinav S

                  Is this a test?

                  The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.

                  M Offline
                  M Offline
                  M dHatter
                  wrote on last edited by
                  #8

                  Yea you didn't pass. Your time expired :P

                  "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                  1 Reply Last reply
                  0
                  • D DaveAuld

                    At a guess, it creates a new method 'code' on the object s, that when called sets up a recursive loop to itself? Ok, it creates a prototype method that when called from a string object reads the string and converts string value to a function that executes and displays a "Hello" message box. just tried it...... :)

                    Dave Find Me On: Web|Facebook|Twitter|LinkedIn


                    Folding Stats: Team CodeProject

                    modified on Sunday, May 15, 2011 7:22 AM

                    M Offline
                    M Offline
                    M dHatter
                    wrote on last edited by
                    #9

                    I can sum it up in two words, Javascript Injection

                    "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                    1 Reply Last reply
                    0
                    • D Daniel Scott

                      Seems to me that it's just going to alert "hello!" once and that's it, but this is probably some kind of trick-question..

                      M Offline
                      M Offline
                      M dHatter
                      wrote on last edited by
                      #10

                      It's a rhetorical question dan. :laugh:

                      "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Thank you very much. We now need an article and some techniques to protect against JavaScript Injection Attacks. :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        M Offline
                        M Offline
                        M dHatter
                        wrote on last edited by
                        #11

                        If i had the time, you can write it and use my code example.

                        "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                        L 1 Reply Last reply
                        0
                        • M M dHatter

                          If i had the time, you can write it and use my code example.

                          "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          I don't think I know enough about the subject to warrant an article. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          M 2 Replies Last reply
                          0
                          • L Luc Pattyn

                            I don't think I know enough about the subject to warrant an article. :)

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            M Offline
                            M Offline
                            M dHatter
                            wrote on last edited by
                            #13

                            Me neither, we should get john to do it

                            "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              I don't think I know enough about the subject to warrant an article. :)

                              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                              M Offline
                              M Offline
                              M dHatter
                              wrote on last edited by
                              #14

                              Here I just made a tip or trick out of it. http://www.codeproject.com/Tips/196535/Javascript-Injection-at-its-Finest-without-even-us.aspx[^]

                              "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                              1 Reply Last reply
                              0
                              • M M dHatter

                                String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

                                var s = 'alert("hello!");'

                                s.code();

                                "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                                modified on Sunday, May 15, 2011 12:54 PM

                                A Offline
                                A Offline
                                Andy Brummer
                                wrote on last edited by
                                #15

                                I don't know why you needed to modify the string prototype, but Function and eval both allow you to dynamically execute javascript. You can also just type it directly into the address bar, use a bookmarklet or a debug console.

                                Curvature of the Mind now with 3D

                                1 Reply Last reply
                                0
                                • M M dHatter

                                  String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

                                  var s = 'alert("hello!");'

                                  s.code();

                                  "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                                  modified on Sunday, May 15, 2011 12:54 PM

                                  B Offline
                                  B Offline
                                  BillWoodruff
                                  wrote on last edited by
                                  #16

                                  Hi VectorX, That code fragment just tells me that JavaScript, like LISP, and PostScript, happens to be a programming language in which the distinction between groups of characters and executable code is trivial to blur. best, Bill

                                  "Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

                                  1 Reply Last reply
                                  0
                                  • M M dHatter

                                    String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

                                    var s = 'alert("hello!");'

                                    s.code();

                                    "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                                    modified on Sunday, May 15, 2011 12:54 PM

                                    V Offline
                                    V Offline
                                    Vic Rauch
                                    wrote on last edited by
                                    #17

                                    And now, to the unexpecting, there are people on facebook asking others on facebook to copy something and paste that into the address bar. It then sends itself to all of the user's facebook friends. Very insidious!

                                    1 Reply Last reply
                                    0
                                    • M M dHatter

                                      String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); };

                                      var s = 'alert("hello!");'

                                      s.code();

                                      "I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein "Few things are harder to put up with than the annoyance of a good example." Mark Twain

                                      modified on Sunday, May 15, 2011 12:54 PM

                                      D Offline
                                      D Offline
                                      dpminusa
                                      wrote on last edited by
                                      #18

                                      I wonder what this lisp is all about. Hmmm... void(0).

                                      "Courtesy is the product of a mature, disciplined mind ... ridicule is lack of the same - DPM"

                                      1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        Thank you very much. We now need an article and some techniques to protect against JavaScript Injection Attacks. :)

                                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                        A Offline
                                        A Offline
                                        Adar Wesley
                                        wrote on last edited by
                                        #19

                                        There is absolutly no need to protect against Javascript injection. Just asume it is not safe. It runs on the "attackers" machine in his/her browser. They can mess with anything they want anyway. Your application defenses should be on the server side anyway. On the other hand, using Javascript's ability to dynamically eval code from string can be extremely powerfull. --- Adar Wesley

                                        L 1 Reply Last reply
                                        0
                                        • A Adar Wesley

                                          There is absolutly no need to protect against Javascript injection. Just asume it is not safe. It runs on the "attackers" machine in his/her browser. They can mess with anything they want anyway. Your application defenses should be on the server side anyway. On the other hand, using Javascript's ability to dynamically eval code from string can be extremely powerfull. --- Adar Wesley

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          I can see your point.

                                          Adar Wesley wrote:

                                          Your application defenses should be on the server side anyway.

                                          Absolutely, the main defenses are the ones on the server, I agree. :)

                                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups