Need to detect which <script> block interact with which other <script> block in a webpage?

152 views Asked by At

I need to check whether javascript in one block, can access or manipulate javascript in any another script block in a webpage. For Example, the second block (inside div) access the first script block inside body.

<body>
<script>
    var first_script_block=0;
</script>
<div>
<script >
    var secondblock_acess_first =first_script_block; 
</script>
</div>
</body>

I though it a lot. I feel horrible. I need some ideas. :(

2

There are 2 answers

0
gdoron On

All the scripts share the same global-object

If you don't want it to happen(it's hard to tell from your question) use closures:

<script>
(function (){
    // here is the code for the first script tag.
})();
</script>
...
<script>
(function (){
    // here is the code for the second script tag
})();
</script>
0
sachleen On

Yes. Check this demo. See, you can create a variable in one and modify it in the other. It doesn't really matter how your JS is split up in script blocks.

Output:

In first block
0
In second block
1