I\'ve a JavaScript file that processes tab switches. Here is the source:
var tCount = 0;
function SwitchToTab(id) {
if (id < 0 || id > tCount) { i
No, this is not possible. The html spec dictates that a <script>
tag does one or the other.
W3 Schools, emphasis mine.
The element either contains scripting statements, or it points to an external script file through the src attribute.
Note: If the "src" attribute is present, the element must be empty.
<script>Tag Html Spec, emphasis mine.
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.
You can't put javascript inside of <script src="...
tags, but you could run a JavaScript file which parses the HTML, looking for <script src
tags, which converts it into two tags.
For example,
<script src="Resources/Tabs.js">InitializeTabs(0, 4);</script>
would have to be converted into
<script src="Resources/Tabs.js"></script>
<script>InitializeTabs(0, 4);</script>
Here's the code I tried:
var tags = document.querySelectorAll("script[src]");
[].forEach.call(tags, function(elem){
var text = elem.innerText;
var src = elem.src;
var parent = elem.parentNode;
parent.removeChild(elem);
var newTag = document.createElement('script');
newTag.setAttribute('src', src);
parent.appendChild(newTag);
var newTag = document.createElement('script');
var t = document.createTextNode(text);
newTag.appendChild(t);
parent.appendChild(newTag);
});
in a JSFiddle
You are suppose to do it the second way. in <script src="Resources/Tabs.js">InitializeTabs(0, 4);</script>
you are referencing an external javascript file, your inline code should go into a second script block.
You can either use src
, or put JavaScript inside the tag.
But not both at once. There's no downside to using two tags anyway (apart from larger file size).
When you include <script src="Resources/Tabs.js"></script>
, you are mentioning that you want to use the Javascript which is included inside the Tabs.js
file so that the compiler knows where to look for InitializeTabs
function, when it is trying to execute the function.
Now if you want to include some Javascript inline, that is inside the HTML, hen you use the <script>... JAVASCRIPT HERE .... </script>
You need to do it like this
<script src="Resources/Tabs.js"></script>
<script>InitializeTabs(0, 4);</script>