admin管理员组

文章数量:1425935

I have a two tags in my page as below:

<![CDATA[
<script type="text/javascript" src="Somejavasrcipt.js"></script> 
<script type="text/javascript"> 
callingThisFunction("Hello-_-Hello"); 
</script>

I tried to remove the two tags and put everything into one, something similar to as below:

<script type="text/javascript" src="Somejavasrcipt.js">
callingThisFunction("Hello-_-Hello"); 
</script>

But when I moved everything under one script tag, the function

callingThisFunction("Hello-_-Hello") 

is not called porperly. Is there any specific reason why it that occuring. Cant we put src attribute in a tag like this. Or what am I doing wrong.

I have a two tags in my page as below:

<![CDATA[
<script type="text/javascript" src="Somejavasrcipt.js"></script> 
<script type="text/javascript"> 
callingThisFunction("Hello-_-Hello"); 
</script>

I tried to remove the two tags and put everything into one, something similar to as below:

<script type="text/javascript" src="Somejavasrcipt.js">
callingThisFunction("Hello-_-Hello"); 
</script>

But when I moved everything under one script tag, the function

callingThisFunction("Hello-_-Hello") 

is not called porperly. Is there any specific reason why it that occuring. Cant we put src attribute in a tag like this. Or what am I doing wrong.

Share Improve this question edited Nov 14, 2013 at 20:08 kei 20.5k2 gold badges37 silver badges64 bronze badges asked Nov 14, 2013 at 20:03 user179516user179516 3374 gold badges11 silver badges20 bronze badges 1
  • The src attribute identifies to the browser both that the Javascript code is to be read from an external file (ignoring the content between the script tags if there is any) and also the name and location of the file that containsd the script source – kei Commented Nov 14, 2013 at 20:07
Add a ment  | 

3 Answers 3

Reset to default 4

Sorry, you can't put script inside script tags with an src. The inner script is ignored and the src code is run.

So, Somejavasrcipt.js is being run, but the inside script, callingThisFunction("Hello-_-Hello");, will be pletely ignored by the parser.

See this MDN Article.

when using the script tag.. you omit the src attribute if you are adding code inside the script tag

https://developer.mozilla/en-US/docs/Web/HTML/Element/script

This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.

You can use the script tag by specifying a src. Or you can use the script tag by providing the element content. You cannot use both:

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. Note that the charset attribute refers to the character encoding of the script designated by the src attribute; it does not concern the content of the SCRIPT element.

http://www.w3/TR/html401/interact/scripts.html

本文标签: javascriptltscriptgt tag src attributeStack Overflow