1

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.

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 Commented Nov 14, 2013 at 20:07

3 Answers 3

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 completely ignored by the parser.

See this MDN Article.

Sign up to request clarification or add additional context in comments.

1 Comment

you can simply eval() the .textContent of the last document.getElementsByTagName("script") if you don't use the defer or async attrib on the script tag.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.