Is the following valid javascript? Would the variable be available to the externally called script?
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, it's not possible to do what you're doing directly. However there's a clever little hack you can employ to enable it: http://ejohn.org/blog/degrading-script-tags/
Consider the following HTML snippet:
<script src="external.js">
var something = "";
</script>
Add the following code to the end of your external.js
file:
var scripts = document.getElementsByTagName("script");
var length = scripts.length;
var script = scripts[length - 1].innerHTML;
eval(script);
Note however that eval
must be called indirectly so that the script is executed in the global scope.
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>
It only executes if it is explicitly processed. See: http://ejohn.org/blog/degrading-script-tags. Specifically:
We can verify these two facts with some tests:
Test 1: Verify that internal scripts aren’t executed, even if an external src is loaded.
Test 2: Verify that internal scripts aren’t executed, even if an external src is not loaded.
Together these indicate that the internal script is never loaded unless a script on the page takes action to eval the inline code.