+ 2

Why can't external script contain script tags?

if, for some reason, you wanted your external script to be an HTML script, why couldn't you have nested script tags? EDIT: for example, what if you do <script type ="text/html" src="a.html"></script> ? could you have <script> tags in a.html? if not, then why not??

17th Jun 2017, 10:04 PM
Carlos Diaz
Carlos Diaz - avatar
10 Réponses
+ 19
I'm not sure if I understood your question correctly, @Carlos, if you happen to speak spanish then we could try that way. But anyways, this is what I understood: - You have 2 html files: One called "a.html" and the other one, let's call it "b.html" - The "a.html" file has inline JavaScript in it, this means, it's using the <script> tag with all the logic and scripting statements inside the opening and closing script tags, like this, for example: <script> document.write("Hello World"); </script> - The "b.html" file, however, it's using the <script> tag to reference an external script, but basically you want to know if you can "call" the inline JavaScript from another html file, in this case, the scripting statement that's inside the script tags from the "a.html" file. Like this, your example: <script src="a.html"> If this was your question. Then the answer is no. Why? 3 reasons: 1) The inline JavaScript is considered a bad practice. So, try to avoid it, in this case the "a.html" file should NOT contain a client-side script (JavaScript) written inline, but rather, it should point to an external script file through the src attribute. Remember, an html file is not a script file because its extension is ".html" which tells the browser that what's inside that file is a "hypertext markup language", a real script file has the ".js" extension. 2) If you want to run the same JavaScript statements on both pages, I mean, on "a.html" and "b.html" files, then, you should create an external JavaScript file (save it with the ".js" extension), instead of writing the same script over and over again, and then refer to it using the src attribute in the <script> tag of both a and b html pages. 3) An external script file CANNOT contain the <script> tag. So in your case, if you wanted to use the "a.html" page as the external script you couldn't do it mainly because of the previous reasons given but also because external scripts cannot contain the html <script> tag, which "a.html" is using.
18th Jun 2017, 12:42 AM
Pao
Pao - avatar
+ 11
Because the external script is saved with a ".js" extension and the <script> tag is an HTML tag, so... Javascript does not recognizes the statement and it will give you an error. However you could have nested <script> tags for example... in single/multiple HTML files.
17th Jun 2017, 10:31 PM
Maz
Maz - avatar
+ 7
@Ben do you mean in an external HTML file, right?
17th Jun 2017, 10:41 PM
Maz
Maz - avatar
+ 7
Oh sure! So, maybe i am wrong since i understood he asked to have multiple ".js" file with the <script> tag inside them... :3 Thank you, now it is more clear! :)
17th Jun 2017, 10:49 PM
Maz
Maz - avatar
+ 5
You could have as many script tags as you wish both inline and external, but it might pose performance issues. And NO we don't nest them but rather as many as you have could be bundle into one with bundlers such as Webpack.
17th Jun 2017, 10:36 PM
Benneth Yankey
Benneth Yankey - avatar
+ 5
I might not have gotten the question right or not sure though, I thought he meant nesting script tags like html structure. i.e <script> <script> </script> </script> that's why I said no. But one could have multiple external script tags pointing to different JavaScript files either in same html or multiple html files like: <script src="a.js"></script> <script src="b.js"></script> <script src="c.js"></script>
17th Jun 2017, 10:47 PM
Benneth Yankey
Benneth Yankey - avatar
+ 5
Nope! @Carlos script tags are for JavaScript and VBScript as far as I know.
17th Jun 2017, 11:29 PM
Benneth Yankey
Benneth Yankey - avatar
+ 2
The syntax needs some editing still, you would need to change the src and the type. example <script type="text/JavaScript" src="scripts/script.js"></script> You can link to another html file with the anchor tag <a href="a.html"></a> not a script tag as html is not a scripting language it's a markup language.
18th Jun 2017, 12:31 AM
Dustin Hammack
Dustin Hammack - avatar
0
I updated the question if you want to check it out. I didn't really ask it well.
17th Jun 2017, 11:27 PM
Carlos Diaz
Carlos Diaz - avatar
0
so the example code I included isn't even proper syntax?
17th Jun 2017, 11:30 PM
Carlos Diaz
Carlos Diaz - avatar