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:
- you define variable called
i
value of1
. you write new script document
2.1. new script redefines
i
3
.2.2. script writes
i
(keep in mind,3
) document.you write
i
again (value still same when redefined it,3
) document.- end result
33
, because wrote3
twice.
order of second block:
- you define variable
i
value1
. - javascript concatenates string ,
i
, creating"<script> i=3; document.write(i); </script>1"
(notice1
@ end, concatenation) javascript writes new concatenated string document.
3.1.
i
redefined3
.3.2.
document.write
'si
(which redefined3
previously).you end
"3<script> i=3; document.write(i); </script>1"
final document, due concatenation ,document.write
. , obviously,<script>
's contents aren't visible. end31
.
Comments
Post a Comment