javascript - Why can scripts inserted with document.write change variables -


why below script printing 33 rather 31?

<script> var = 1; document.write("<script> i=3; document.write(i); </scr" + "ipt>"); document.write(i); </script> 

this 1 prints 31, difference?

<script> var = 1; document.write("<script> i=3; document.write(i); </scr" + "ipt>" + i); </script> 

this order of how it's working:

  1. you define variable called i value of 1.
  2. you write new script document

    2.1. new script redefines i 3.

    2.2. script writes i (keep in mind, 3) document.

  3. you write i again (value still same when redefined it, 3) document.

  4. end result 33, because wrote 3 twice.


order of second block:

  1. you define variable i value 1.
  2. javascript concatenates string , i, creating "<script> i=3; document.write(i); </script>1" (notice 1 @ end, concatenation)
  3. javascript writes new concatenated string document.

    3.1. i redefined 3.

    3.2. document.write's i (which redefined 3 previously).

  4. you end "3<script> i=3; document.write(i); </script>1" final document, due concatenation , document.write. , obviously, <script>'s contents aren't visible. end 31.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -