Having a Script in a Script Src Tag? <script> (JavaScript, etc.)

Why is this not working?

<script type="text/javascript" src="//cdn.com/assets/js/jquery.js"> alert("Hello World!"); </script> 

But does it?

 <script type="text/javascript" src="//cdn.com/assets/js/jquery.js"></script> <script type="text/javascript"> alert("Hello World!"); </script> 

This is common to many HTML tags that are extracted from source code. Micro-optimization is important in my situation, and I'm also interested.

+4
source share
3 answers

From w3.org (my attention):

If the src value has a URI value, user agents should ignore the content element and get the script through the URI.

+8
source

from http://javascript.crockford.com/script.html :

"If the src attribute is missing, the content text between <script> and </script> compiled and executed."

Since the src attribute exists, the content is not executed

+7
source

In the first example, you define src , which makes it IGNORE the contents of <script></script>

In the second example, you have 2 separate <script></script> , the second of which contains your code to execute.

+1
source

All Articles