Fail silently without throwing errors.

Why Using Inline JavaScript in Script Tag Doesn't Work

The reason for this behavior is that a script element can only load a single source, be it external or inline. When both src and inline content are present, the inline content is ignored. Therefore:

Will not load the specified scripts.

Solution: Using Multiple Script Elements

To load multiple scripts, you need to use separate script elements for each:

Alternatively, you can create a parent script that dynamically loads the necessary scripts:

Additional Notes

While inline JavaScript is ignored in script elements with external sources, the content of the script element remains in the DOM. This has prompted some developers to use it for storing data that is accessed by the external scripts. However, using data-* attributes is generally a more appropriate and cleaner approach for this purpose.

","image":"http://www.luping.net/uploads/20241119/1731989169673c0eb13ec4e.jpg","datePublished":"2024-11-19T12:51:39+08:00","dateModified":"2024-11-19T12:51:39+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why is Inline JavaScript Not Working in Script Tags with External Sources?

Why is Inline JavaScript Not Working in Script Tags with External Sources?

Published on 2024-11-19
Browse:987

Why is Inline JavaScript Not Working in Script Tags with External Sources?

Loading Scripts with HTML Script Tags

The HTML script tag is used to include and execute JavaScript code. By default, this tag takes an external source attribute, such as scr to load a JavaScript file. However, attempts to include inline JavaScript within the script tag, like this:

Fail silently without throwing errors.

Why Using Inline JavaScript in Script Tag Doesn't Work

The reason for this behavior is that a script element can only load a single source, be it external or inline. When both src and inline content are present, the inline content is ignored. Therefore:

Will not load the specified scripts.

Solution: Using Multiple Script Elements

To load multiple scripts, you need to use separate script elements for each:

Alternatively, you can create a parent script that dynamically loads the necessary scripts:

Additional Notes

While inline JavaScript is ignored in script elements with external sources, the content of the script element remains in the DOM. This has prompted some developers to use it for storing data that is accessed by the external scripts. However, using data-* attributes is generally a more appropriate and cleaner approach for this purpose.

Release Statement This article is reprinted at: 1729692525 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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