Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the global scope of <script> tag?

Tags:

javascript

I had made flag and made my previous question deleted because of missunderstanding.

I'm working on a classic asp project.

let's say you have so many <script></script> tags in your code.

For instance:

line 10: <script> ..function 1 definition here..</script>

line 200: <script> .. function 2 definition here..</script>

line 5000: <script> ..function 3 definition here..</script>

also at line 6000: I have another tag which is trying to call function1.

is that possible without using *.js file ?

For instance:

line 6000:

<script> function1(); </script>

Those scripts are not defined in <head> tag.

I know its not useful but I need to know is there any way of it or not.

Hope its more clear now!

like image 790
curiousBoy Avatar asked Jul 05 '13 17:07

curiousBoy


2 Answers

anything inside the script tags gets run immediately. if you define function a() in your first script element, then it will add a function called a to your global namespace. any javascript you execute later on in other script elements will have access to it.

<script type="text/javascript">
   function a() {
       alert('hi');
   }
</script>

...

<script type="text/javascript">
    a();
</script>
like image 72
Kevin Nacios Avatar answered Sep 28 '22 17:09

Kevin Nacios


Yes, that is possible, assuming function1 is in the global scope (e.g. not in a wrapper function/self-invoking function).

like image 24
Sumurai8 Avatar answered Sep 28 '22 17:09

Sumurai8