javascript - Isolating JS from other JS in the same HTML document -


consider code:

<script>var foo = 'bar';</script> <div id='isolated'>     <script>console.log(foo)</script> </div> 

this return 'bar' console. i'd foo , other variables declared outside #isolated undefined within it.

how can achieved? cross-browser, lightweight , native solution preferable.

that's not how javascript works. doesn't scope because of html element block.

you're declaring foo @ global scope, declare in scope used when needed. using es6 (harmony), using let instead of var (proposed, not supported) declare variable proposes block level variable instead of global scoped variable.

as stands however, javascript has no idea foo being logged inside isolated div unless use logic.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -