Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script tags with src and code between script tags

Tags:

javascript

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.

like image 838
Matrix Web Solutions Avatar asked Oct 22 '13 05:10

Matrix Web Solutions


People also ask

What is src in script tag?

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.

What goes between script tags?

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.

Can I have 2 script tags?

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.

Can I add two script tags in HTML?

Yes, we can write any number of tags inside tag. And browser access them in sequential order.


2 Answers

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.

like image 89
rajesh kakawat Avatar answered Nov 11 '22 22:11

rajesh kakawat


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>
like image 26
Niczem Olaske Avatar answered Nov 12 '22 00:11

Niczem Olaske