Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to put "use strict" in external js files if our html file already has "use strict"?

Tags:

javascript

Do we need to put "use strict" in external js files if our html file (which imports the external js files) already has "use strict" ?

And if our external js files do not have "use strict", are they still "strict" within a HTML file that has "use strict" ?

Example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        "use strict";
        function f() {
            // calling File1 functions (File1 does not have "use strict"; at the top)
            // are the File1 functions "strict"?
        }
    </script>
    <script src="File1.js"></script>
    <script>
        //by the way.. is it strict here ?
    </script>
</head>
<body>
</body>
</html>
like image 385
Li Juan Avatar asked Jul 05 '11 06:07

Li Juan


People also ask

Should I use strict mode JavaScript?

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.

Can I write JavaScript both within HTML and also as a separate file?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

What is the correct way to include an external JS file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can JavaScript code can be stored external to an HTML file?

JavaScript in External FileThe script tag provides a mechanism to allow you to store JavaScript in an external file and then include it into your HTML files.


1 Answers

You must put "use strict"; (or 'use strict';) at the top of each script (or function) to make them strict. In your example, the functions in File1.js will not be strict, nor will the second block. See https://developer.mozilla.org/en/JavaScript/Strict_mode#Invoking_strict_mode for details.

If this wasn't the case, using strict mode could invalidate third-party scripts that you import, so it makes sense that strictness only applies to the scripts and individual functions that you explicitly specify.

For example:

external.js:

console.log("C: This script is non-strict.");

var g = function (x) {
    console.log("g is non-strict regardless of caller.");
    return 2 * x;
};

test.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        "use strict";
        console.log("A: This script element is strict.");
        function f() {
            console.log("The strictness of a script does not affect" +
                    " the strictness of external scripts, so g is" +
                    " still non-strict when called from f.");
            return g(3);
        }
    </script>
    <script src="external.js"></script>
    <script>
        f();
        console.log("B: This script element is non-strict.")
    </script>
</head>
<body>
</body>
</html>
like image 89
cwb Avatar answered Oct 12 '22 23:10

cwb