Is the following valid javascript? Would the variable be available to the externally called script?
<script src="//blah">
var something = "";
</script>
Background: I have seen this used in auto generated analytic code (not google) and it really annoys me so wanting to know if i can fix this or if this variable will somehow be used by the externally referenced script.
The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.
In HTML, JavaScript code is inserted between <script> and </script> tags. 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.
An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.
Yes, we can write any number of tags inside tag. And browser access them in sequential order.
Read this, http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1
The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI. Note that the charset attribute refers to the character encoding of the script designated by the src attribute; it does not concern the content of the SCRIPT element.
No its not, if you refer to a source the code between the script tags is ignored. You could do it like this:
<script src="bla.js"></script>
<script>
var something = "";
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With