0

I have following code

<body> 
    <script> 
          (function(s) { })
          ("document.write('<script>document.write(\" <script>document.write('<script>document.write(\"Hello World\");<\/script>');<\/script>\");<\/script>')"); 
    </script> 
</body>

Here in function body what code shall I write so that code here to parses and print Hello World". Here solution must work for n level of document.write() nesting. To be frank I do not have any idea how to do it . Can someone please provide me solution

captainsac
  • 2,484
  • 3
  • 27
  • 48
user1121210
  • 83
  • 1
  • 4
  • 14
  • Hello, Why do you want to do a nested 'document.write()' call? As far as i know your call will be overwritten by nesting 'document.write()' Maybe the purpose of usage makes more clear how to write the code. And what syntax method is that?;) – Nirmi Jun 02 '15 at 11:44
  • Nested `document.write()'` means `document.write(' – user1121210 Jun 02 '15 at 11:46
  • Afaik that would work, because the javascript parser wouldnt parse the nested code. or am i wrong? – Nirmi Jun 02 '15 at 12:28
  • It works but I don't know how. That is why I posted this question – user1121210 Jun 02 '15 at 14:36

2 Answers2

0

Sorry i would say that this makes no sense as you only try to nesting 'document.write()' The Invocation of 'document.write()' will cause that your whole document will be overwritten with the write parameter. But the javascript engine wont parse the content again. So the upcoming nested code wont be run.

Perhabs some other purpose except document.write() could match. But i dont know such a case.

If you are trying to nesting function in JS try

function foo(doBar)
{
  function bar()
  {
    console.log( 'bar' );
  }

  function baz()
  {
    console.log( 'baz' );
  }

  window.baz = baz;
  if ( doBar ) bar();
}

As mentioned here

Community
  • 1
  • 1
Nirmi
  • 1,099
  • 1
  • 8
  • 20
0
 <!doctype html>
    <html> 
        <head>
            <title>Nested document.write()</title> 
        </head>
    <body> 
        <script> 
            //(function(s) { write your code here to parse s and print     "Hello World". Remember - your solution must work for n level of document.write() nesting })( "document.write('<script>document.write(\"<script>document.write('<script>document.write(\"Hello World\");<\/script>');<\/script>\");<\/script>')");

                   (function(s){
                       s = "document.write('<script>document.write(\"<script>document.write('<script>document.write(\"Hello World\");<\/script>\");<\/script>\");<\/script>')";
                       var str, scriptEle = document.createElement('script');
                       str = s.replace(/["']/g, '"');
                       str = str.replace(/"<script>document.write/g, "");
                       str = str.replace(/;<\/script\>"/g, "");
                       scriptEle.innerHTML = str;
                       document.body.appendChild(scriptEle);
                    })();
        </script> 
    </body> 
</html>
user1121210
  • 83
  • 1
  • 4
  • 14