Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript defines scope by script tags?

Tags:

Never met this problem, and don't know why. The only explanation is a scope issue.

In the same page, I have 2 sections of JS :

...
 <script type="text/javascript">
    go();
  </script>

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

This will show an error : go is not defined

where

...
     <script type="text/javascript">
        go();

        function go()
        { alert('');  }
      </script>
    ...

is working (obviously).

Does <script> tag creates a scope of JS ? help ?

like image 769
Royi Namir Avatar asked Jun 10 '12 13:06

Royi Namir


People also ask

Are script tags scoped?

Strict Mode Settings In JavaScript Are Scoped To The Script Tag.

What is script scope in JavaScript?

In JavaScript, scopes are created by code blocks, functions, modules. While const and let variables are scoped by code blocks, functions or modules, var variables are scoped only by functions or modules. Scopes can be nested. Inside an inner scope you can access the variables of an outer scope.

What is script tag in JavaScript?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Does JavaScript have scope?

JavaScript variables have only two scopes. Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined.


2 Answers

This isn't a scope issue. If you define a function (in the global scope) in one script element, then you can use it in another.

However, script elements are parsed and executed as they are encountered.

Hoisting won't work across script elements. A function defined in a later script element won't be available during the initial run of an earlier script element.

You either need to swap the order of your script elements, or delay the function call until after the script that defines it has run (e.g. by attaching it to an onload event handler).

<script>
    function go() {
        alert('');  
    }
</script>
<script>
    go();
</script>

or

<script>
    window.addEventListener("load", function () { 
        go();
    }, false);
</script>
<script>
    function go() {
        alert('');  
    }
</script>
like image 80
Quentin Avatar answered Oct 16 '22 00:10

Quentin


The html parser stops to execute your script before moving to next elements. So the next script element is not executed until the first one is executed.

This is comparable to:

<script>
document.getElementById("hello") //null because the html parser hasn't met the div yet.
</script>
<div id="hello"></div>
like image 37
Esailija Avatar answered Oct 16 '22 00:10

Esailija