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

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -

Desktop Launcher for Python Script Starts Program in Wrong Path (Linux) -