Can You Embed Inline Script in an External JavaScript File Using the SRC Attribute?
While JavaScript is typically included using an external script file, a common question arises: can an inline script be incorporated within that external file using the src attribute?
According to the HTML 4.01 specification:
"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."
In other words, it's either an inline script or an external script loaded via the src attribute. The two methods cannot coexist in the same script tag.
Clarification:
The src attribute always takes precedence over the inline script code. If a script tag includes both a src attribute and an inline script, browsers will disregard the inline script and only load the external JavaScript file specified by the src attribute.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3