简体   繁体   中英

<script> tag src attribute

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.

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 completely 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.org/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.org/TR/html401/interact/scripts.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM