Tags?", "text": "<p>I've a JavaScript file that processes tab switches. Here is the source:</p>\n\n<pre class="prettyprint"><code>var tCount = 0;\n\nfunction SwitchToTab(id) {\n if (id &lt; 0 || id &gt; tCount) { id = 0; }\n\n for (var i = 0; i &lt; tCount; i++) { document.getElementById("tab" + i).className = ""; }\n document.getElementById("tab" + id).className = "active";\n\n for (var i = 0; i &lt; tCount; i++) { document.getElementById("area" + i).style.display = "none"; }\n document.getElementById("area" + id).style.display = "";\n}\n\nfunction InitializeTabs(initialTabId, tabsCount) {\n tCount = tabsCount;\n SwitchToTab(initialTabId);\n}\n</code></pre>\n\n<p>I'm trying to make it as short as possible like this:</p>\n\n<pre class="prettyprint"><code>&lt;script src="Resources/Tabs.js"&gt;InitializeTabs(0, 4);&lt;/script&gt;\n</code></pre>\n\n<p>It doesn't works but it works if I separate them like this:</p>\n\n<pre class="prettyprint"><code>&lt;script src="Resources/Tabs.js"&gt;&lt;/script&gt;\n&lt;script&gt;InitializeTabs(0, 4);&lt;/script&gt;\n</code></pre>\n\n<p>So, is there any way to run JavaScript inside <code>&lt;script src="..."&gt;&lt;/script&gt;</code> tags? What I am missing?</p>", "answerCount": 1, "upvoteCount": 210, "dateCreated": "2015-04-02 13:21:17", "dateModified": "2022-10-02 11:52:49", "author": { "type": "Person", "name": "MFatihMAR" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>No, this is not possible. The html spec dictates that a <code>&lt;script&gt;</code> tag does one or the other.</p>\n<p><code>&lt;script&gt;</code>Tag Html Spec, emphasis mine.</p>\n<blockquote>\n<p>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. <strong>If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.</strong></p>\n</blockquote>", "upvoteCount": 110, "url": "https://exchangetuts.com/run-javascript-inside-script-srcscript-tags-1640541004494441#answer-1652926689383519", "dateCreated": "2022-09-30 23:52:49", "dateModified": "2022-10-02 11:52:49", "author": { "type": "Person", "name": "DLeh" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JavaScript inside <script src="..."></script> Tags?

I've a JavaScript file that processes tab switches. Here is the source:

var tCount = 0;

function SwitchToTab(id) {
    if (id < 0 || id > tCount) { id = 0; }

    for (var i = 0; i < tCount; i++) { document.getElementById("tab" + i).className = ""; }
    document.getElementById("tab" + id).className = "active";

    for (var i = 0; i < tCount; i++) { document.getElementById("area" + i).style.display = "none"; }
    document.getElementById("area" + id).style.display = "";
}

function InitializeTabs(initialTabId, tabsCount) {
    tCount = tabsCount;
    SwitchToTab(initialTabId);
}

I'm trying to make it as short as possible like this:

<script src="Resources/Tabs.js">InitializeTabs(0, 4);</script>

It doesn't works but it works if I separate them like this:

<script src="Resources/Tabs.js"></script>
<script>InitializeTabs(0, 4);</script>

So, is there any way to run JavaScript inside <script src="..."></script> tags? What I am missing?

like image 210
MFatihMAR Avatar asked Apr 02 '15 13:04

MFatihMAR


1 Answers

No, this is not possible. The html spec dictates that a <script> tag does one or the other.

<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.

like image 110
DLeh Avatar answered Oct 02 '22 11:10

DLeh