I noticed that the
<script src="myFile.js"> alert( "This is a test" ); </script>
And this does not work, nor does it throw an error in FireBug, why is this happening, why do we have to add extra
<script> tags to allow for JS to be used on the form itself?
I have a file, found @
script/addScript.js from my root, and this contains:
This is designed to allow me to add scripts to the DOM quickly and effectively, so I tried to do this:
<script src="script/addScript.js"> addScript( "script/obj.js" ); addScript( "script/home/login.js" ); </script>
But it did not work, so I have to do this instead:
<script> addScript( "script/obj.js" ); addScript( "script/home/login.js" ); </script>
A script element loads a single script.
It can do that from either a URL or from inline, but it still loads a script so it can't do both.
If you try to do both, the inline script will be ignored:
<script src="example.js"> this_is_ignored(); </script>
… so you need multiple script elements.
<script src="example.js"> </script> <script> this_is_NOT_ignored(); </script>
It is worth noting that the content of the script element will still exist in the DOM, so some people use it to store data in it which the JS referenced by the src will read.
data-* attributes are (arguably) a cleaner approach to that though.
src attribute to load an external file, that is the piece of executable js for that element, otherwise it is the code placed between the
As to why this is the case, it was likely a design choice by whoever established this standard. By creating a one-to-one relationship between code pieces and
Therefore in your case you will first have to load your external file...
and then call any functions provided by it.
addScript( "script/obj.js" );
addScript( "script/home/login.js" );
©2020 All rights reserved.